diff --git a/.ci/run_e2e_tests.sh b/.ci/run_e2e_tests.sh new file mode 100755 index 0000000..82126d1 --- /dev/null +++ b/.ci/run_e2e_tests.sh @@ -0,0 +1,16 @@ +SCRIPTS_PATH=$(cd $(dirname "${BASH_SOURCE[0]}"); pwd) +SRC_PATH=$(dirname "${SCRIPTS_PATH}") +# Build the runnable Gecko docker image +bash "${SRC_PATH}"/scripts/build_image.sh +GECKO_IMAGE=$(docker image ls --format="{{.Repository}}" | head -n 1) + +# Turn off GO111MODULE to pull e2e test source code in order to get run script. +GO111MODULE=off go get -t -v github.com/kurtosis-tech/ava-e2e-tests/... +cd "${GOPATH}"/src/github.com/kurtosis-tech/ava-e2e-tests/ || exit + +bash "./scripts/rebuild_initializer_binary.sh" +bash "./scripts/rebuild_controller_image.sh" +# TODO: Make the controller image label a parameter to rebuild_controller_image script +# Standard controller image label used by above scripts. +CONTROLLER_IMAGE="kurtosistech/ava-e2e-tests_controller:latest" +./build/ava-e2e-tests --gecko-image-name="${GECKO_IMAGE}" --test-controller-image-name="${CONTROLLER_IMAGE}" --test-names="fiveStakingNodeGetValidatorsTest,fiveStakingNodeFullyConnectedTest" diff --git a/.travis.yml b/.travis.yml index a919fa0..b06f3c2 100644 --- a/.travis.yml +++ b/.travis.yml @@ -14,6 +14,7 @@ env: global: - CODECOV_TOKEN="8c18c993-fc6e-4706-998b-01ddc7987804" - GECKO_HOME=/go/src/github.com/ava-labs/gecko/ + - E2E_TEST_HOME=/go/src/github.com/kurtosis-tech/ava-e2e-tests/ - COMMIT=${TRAVIS_COMMIT::8} - DOCKERHUB_REPO=avaplatform/gecko - secure: "L/A9+re0NEKP6EV6H9RcTGiDhX3WMvsiWrkRKDYKqnviqbjY30RK6EM4vvjrM4Lrw2QwsO3YKgnku3+zioE/TxEZFkpkbjNUXru0nYBrWAg1TKVsDXnYaIZkHUejfryST3E8N7F4Hx6zCtGEO0sEdUeKuT+MNUIuHezHooTgGzDjMogm70EWMFjQHc7VucTJu7dWU1RBPjovWQ0q9qflrtCpbrvXFIiihQQ1PQha1Q2C4wLakKuLbhhSafue90Mnyss0blaPHy/tyewcASJu4vsGTKRBn0DzttlkNTwuD6+nKrbmJY0ohunnkVFzYjrZAw1gyN+DCDb/lPbz4ZDItKPwrIUPEtL5xuUOrxUZPUh+0io3Q2d6rjaqkdGjd1KQXzbnW1mn0BxX3d3b2UpIqhBn9umYYjHBKnMuoRiTK33b7U9+LF3K84+tEvVDCPeHs/mw6Inp5jGRSravnM6yPQ6feGzogs4+3EMzZXxnkngKFKCsnd67Oe9xfV9amOU2aQAx4jaAwlPjEpBEkUa8YKx3lPznvmUk1QsNCUbLjdSl5JBaXojLJoiuPbj29hp4S5AXXgn+3Hvwk3ndcFCxi6/l1W9mjYSOtFqg3EAUdF4EgnA/ykQg9ZokkoKY0+qgOzG2bKOAYuCDWeGr7P1apToh00ccsQXL81nVPiq7uDw=" @@ -26,7 +27,7 @@ install: script: - if [ "$TRAVIS_OS_NAME" = "osx" ]; then .ci/runscript_osx.sh; fi - - if [ "$TRAVIS_OS_NAME" = "linux" ]; then .ci/runscript_linux.sh; fi + - if [ "$TRAVIS_OS_NAME" = "linux" ]; then .ci/runscript_linux.sh; .ci/run_e2e_tests.sh; fi #Need to push to docker hub only from one build after_success: diff --git a/api/admin/performance.go b/api/admin/performance.go index bf2a460..6035b08 100644 --- a/api/admin/performance.go +++ b/api/admin/performance.go @@ -10,6 +10,15 @@ import ( "runtime/pprof" ) +const ( + // Name of file that CPU profile is written to when StartCPUProfiler called + cpuProfileFile = "cpu.profile" + // Name of file that memory profile is written to when MemoryProfile called + memProfileFile = "mem.profile" + // Name of file that lock profile is written to + lockProfileFile = "lock.profile" +) + var ( errCPUProfilerRunning = errors.New("cpu profiler already running") errCPUProfilerNotRunning = errors.New("cpu profiler doesn't exist") @@ -20,12 +29,12 @@ var ( type Performance struct{ cpuProfileFile *os.File } // StartCPUProfiler starts measuring the cpu utilization of this node -func (p *Performance) StartCPUProfiler(filename string) error { +func (p *Performance) StartCPUProfiler() error { if p.cpuProfileFile != nil { return errCPUProfilerRunning } - file, err := os.Create(filename) + file, err := os.Create(cpuProfileFile) if err != nil { return err } @@ -52,8 +61,8 @@ func (p *Performance) StopCPUProfiler() error { } // MemoryProfile dumps the current memory utilization of this node -func (p *Performance) MemoryProfile(filename string) error { - file, err := os.Create(filename) +func (p *Performance) MemoryProfile() error { + file, err := os.Create(memProfileFile) if err != nil { return err } @@ -66,8 +75,8 @@ func (p *Performance) MemoryProfile(filename string) error { } // LockProfile dumps the current lock statistics of this node -func (p *Performance) LockProfile(filename string) error { - file, err := os.Create(filename) +func (p *Performance) LockProfile() error { + file, err := os.Create(lockProfileFile) if err != nil { return err } diff --git a/api/admin/service.go b/api/admin/service.go index e05a440..fa48282 100644 --- a/api/admin/service.go +++ b/api/admin/service.go @@ -57,7 +57,7 @@ type GetNodeVersionReply struct { // GetNodeVersion returns the version this node is running func (service *Admin) GetNodeVersion(_ *http.Request, _ *struct{}, reply *GetNodeVersionReply) error { - service.log.Debug("Admin: GetNodeVersion called") + service.log.Info("Admin: GetNodeVersion called") reply.Version = service.version.String() return nil @@ -70,7 +70,7 @@ type GetNodeIDReply struct { // GetNodeID returns the node ID of this node func (service *Admin) GetNodeID(_ *http.Request, _ *struct{}, reply *GetNodeIDReply) error { - service.log.Debug("Admin: GetNodeID called") + service.log.Info("Admin: GetNodeID called") reply.NodeID = service.nodeID return nil @@ -83,7 +83,7 @@ type GetNetworkIDReply struct { // GetNetworkID returns the network ID this node is running on func (service *Admin) GetNetworkID(_ *http.Request, _ *struct{}, reply *GetNetworkIDReply) error { - service.log.Debug("Admin: GetNetworkID called") + service.log.Info("Admin: GetNetworkID called") reply.NetworkID = cjson.Uint32(service.networkID) return nil @@ -96,7 +96,7 @@ type GetNetworkNameReply struct { // GetNetworkName returns the network name this node is running on func (service *Admin) GetNetworkName(_ *http.Request, _ *struct{}, reply *GetNetworkNameReply) error { - service.log.Debug("Admin: GetNetworkName called") + service.log.Info("Admin: GetNetworkName called") reply.NetworkName = genesis.NetworkName(service.networkID) return nil @@ -114,7 +114,7 @@ type GetBlockchainIDReply struct { // 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.Debug("Admin: GetBlockchainID called") + service.log.Info("Admin: GetBlockchainID called") bID, err := service.chainManager.Lookup(args.Alias) reply.BlockchainID = bID.String() @@ -128,26 +128,21 @@ type PeersReply struct { // Peers returns the list of current validators func (service *Admin) Peers(_ *http.Request, _ *struct{}, reply *PeersReply) error { - service.log.Debug("Admin: Peers called") + 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"` -} - // StartCPUProfilerReply are the results from calling StartCPUProfiler type StartCPUProfilerReply struct { Success bool `json:"success"` } // StartCPUProfiler starts a cpu profile writing to the specified file -func (service *Admin) StartCPUProfiler(_ *http.Request, args *StartCPUProfilerArgs, reply *StartCPUProfilerReply) error { - service.log.Debug("Admin: StartCPUProfiler called with %s", args.Filename) +func (service *Admin) StartCPUProfiler(_ *http.Request, args *struct{}, reply *StartCPUProfilerReply) error { + service.log.Info("Admin: StartCPUProfiler called") reply.Success = true - return service.performance.StartCPUProfiler(args.Filename) + return service.performance.StartCPUProfiler() } // StopCPUProfilerReply are the results from calling StopCPUProfiler @@ -157,31 +152,21 @@ type StopCPUProfilerReply struct { // StopCPUProfiler stops the cpu profile func (service *Admin) StopCPUProfiler(_ *http.Request, _ *struct{}, reply *StopCPUProfilerReply) error { - service.log.Debug("Admin: StopCPUProfiler called") + service.log.Info("Admin: StopCPUProfiler called") reply.Success = true return service.performance.StopCPUProfiler() } -// MemoryProfileArgs are the arguments for calling MemoryProfile -type MemoryProfileArgs struct { - Filename string `json:"filename"` -} - // MemoryProfileReply are the results from calling MemoryProfile type MemoryProfileReply struct { Success bool `json:"success"` } // MemoryProfile runs a memory profile writing to the specified file -func (service *Admin) MemoryProfile(_ *http.Request, args *MemoryProfileArgs, reply *MemoryProfileReply) error { - service.log.Debug("Admin: MemoryProfile called with %s", args.Filename) +func (service *Admin) MemoryProfile(_ *http.Request, args *struct{}, reply *MemoryProfileReply) error { + service.log.Info("Admin: MemoryProfile called") reply.Success = true - return service.performance.MemoryProfile(args.Filename) -} - -// LockProfileArgs are the arguments for calling LockProfile -type LockProfileArgs struct { - Filename string `json:"filename"` + return service.performance.MemoryProfile() } // LockProfileReply are the results from calling LockProfile @@ -190,10 +175,10 @@ type LockProfileReply struct { } // LockProfile runs a mutex profile writing to the specified file -func (service *Admin) LockProfile(_ *http.Request, args *LockProfileArgs, reply *LockProfileReply) error { - service.log.Debug("Admin: LockProfile called with %s", args.Filename) +func (service *Admin) LockProfile(_ *http.Request, args *struct{}, reply *LockProfileReply) error { + service.log.Info("Admin: LockProfile called") reply.Success = true - return service.performance.LockProfile(args.Filename) + return service.performance.LockProfile() } // AliasArgs are the arguments for calling Alias @@ -209,7 +194,7 @@ type AliasReply struct { // Alias attempts to alias an HTTP endpoint to a new name func (service *Admin) Alias(_ *http.Request, args *AliasArgs, reply *AliasReply) error { - service.log.Debug("Admin: Alias called with URL: %s, Alias: %s", args.Endpoint, args.Alias) + service.log.Info("Admin: Alias called with URL: %s, Alias: %s", args.Endpoint, args.Alias) reply.Success = true return service.httpServer.AddAliasesWithReadLock(args.Endpoint, args.Alias) } @@ -227,7 +212,7 @@ type AliasChainReply struct { // AliasChain attempts to alias a chain to a new name func (service *Admin) AliasChain(_ *http.Request, args *AliasChainArgs, reply *AliasChainReply) error { - service.log.Debug("Admin: AliasChain called with Chain: %s, Alias: %s", args.Chain, args.Alias) + service.log.Info("Admin: AliasChain called with Chain: %s, Alias: %s", args.Chain, args.Alias) chainID, err := service.chainManager.Lookup(args.Chain) if err != nil { diff --git a/api/health/checks.go b/api/health/checks.go index d11544b..20dd6b1 100644 --- a/api/health/checks.go +++ b/api/health/checks.go @@ -20,36 +20,66 @@ type CheckFn func() (interface{}, error) // Check defines a single health check that we want to monitor and consider as // part of our wider healthiness -type Check struct { +type Check interface { // Name is the identifier for this check and must be unique among all Checks - Name string + Name() string - // CheckFn is the function to call to perform the the health check - CheckFn CheckFn + // Execute performs the health check. It returns nil if the check passes. + // It can also return additional information to marshal and display to the caller + Execute() (interface{}, error) // ExecutionPeriod is the duration to wait between executions of this Check - ExecutionPeriod time.Duration + ExecutionPeriod() time.Duration // InitialDelay is the duration to wait before executing the first time - InitialDelay time.Duration + InitialDelay() time.Duration // InitiallyPassing is whether or not to consider the Check healthy before the // initial execution - InitiallyPassing bool + InitiallyPassing() bool } -// gosundheitCheck implements the health.Check interface backed by a CheckFn -type gosundheitCheck struct { - name string - checkFn CheckFn +// check implements the Check interface +type check struct { + name string + checkFn CheckFn + executionPeriod, initialDelay time.Duration + initiallyPassing bool } -// Name implements the health.Check interface by returning a unique name -func (c gosundheitCheck) Name() string { return c.name } +// Name is the identifier for this check and must be unique among all Checks +func (c check) Name() string { return c.name } -// Execute implements the health.Check interface by executing the checkFn and -// returning the results -func (c gosundheitCheck) Execute() (interface{}, error) { return c.checkFn() } +// Execute performs the health check. It returns nil if the check passes. +// It can also return additional information to marshal and display to the caller +func (c check) Execute() (interface{}, error) { return c.checkFn() } + +// ExecutionPeriod is the duration to wait between executions of this Check +func (c check) ExecutionPeriod() time.Duration { return c.executionPeriod } + +// InitialDelay is the duration to wait before executing the first time +func (c check) InitialDelay() time.Duration { return c.initialDelay } + +// InitiallyPassing is whether or not to consider the Check healthy before the initial execution +func (c check) InitiallyPassing() bool { return c.initiallyPassing } + +// monotonicCheck is a check that will run until it passes once, and after that it will +// always pass without performing any logic. Used for bootstrapping, for example. +type monotonicCheck struct { + passed bool + check +} + +func (mc monotonicCheck) Execute() (interface{}, error) { + if mc.passed { + return nil, nil + } + details, pass := mc.check.Execute() + if pass == nil { + mc.passed = true + } + return details, pass +} // Heartbeater provides a getter to the most recently observed heartbeat type Heartbeater interface { diff --git a/api/health/service.go b/api/health/service.go index db33640..522ad62 100644 --- a/api/health/service.go +++ b/api/health/service.go @@ -7,15 +7,17 @@ import ( "net/http" "time" - "github.com/AppsFlyer/go-sundheit" + health "github.com/AppsFlyer/go-sundheit" + + "github.com/gorilla/rpc/v2" + "github.com/ava-labs/gecko/snow/engine/common" "github.com/ava-labs/gecko/utils/json" "github.com/ava-labs/gecko/utils/logging" - "github.com/gorilla/rpc/v2" ) // defaultCheckOpts is a Check whose properties represent a default Check -var defaultCheckOpts = Check{ExecutionPeriod: time.Minute} +var defaultCheckOpts = check{executionPeriod: time.Minute} // Health observes a set of vital signs and makes them available through an HTTP // API. @@ -36,7 +38,18 @@ func (h *Health) Handler() *common.HTTPHandler { newServer.RegisterCodec(codec, "application/json") newServer.RegisterCodec(codec, "application/json;charset=UTF-8") newServer.RegisterService(h, "health") - return &common.HTTPHandler{LockOptions: common.NoLock, Handler: newServer} + handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.Method == http.MethodGet { // GET request --> return 200 if getLiveness returns true, else 503 + if _, healthy := h.health.Results(); healthy { + w.WriteHeader(http.StatusOK) + } else { + w.WriteHeader(http.StatusServiceUnavailable) + } + } else { + newServer.ServeHTTP(w, r) // Other request --> use JSON RPC + } + }) + return &common.HTTPHandler{LockOptions: common.NoLock, Handler: handler} } // RegisterHeartbeat adds a check with default options and a CheckFn that checks @@ -48,18 +61,27 @@ func (h *Health) RegisterHeartbeat(name string, hb Heartbeater, max time.Duratio // RegisterCheckFunc adds a Check with default options and the given CheckFn func (h *Health) RegisterCheckFunc(name string, checkFn CheckFn) error { check := defaultCheckOpts - check.Name = name - check.CheckFn = checkFn + check.name = name + check.checkFn = checkFn + return h.RegisterCheck(check) +} + +// RegisterMonotonicCheckFunc adds a Check with default options and the given CheckFn +// After it passes once, its logic (checkFunc) is never run again; it just passes +func (h *Health) RegisterMonotonicCheckFunc(name string, checkFn CheckFn) error { + check := monotonicCheck{check: defaultCheckOpts} + check.name = name + check.checkFn = checkFn return h.RegisterCheck(check) } // RegisterCheck adds the given Check func (h *Health) RegisterCheck(c Check) error { return h.health.RegisterCheck(&health.Config{ - InitialDelay: c.InitialDelay, - ExecutionPeriod: c.ExecutionPeriod, - InitiallyPassing: c.InitiallyPassing, - Check: gosundheitCheck{c.Name, c.CheckFn}, + InitialDelay: c.InitialDelay(), + ExecutionPeriod: c.ExecutionPeriod(), + InitiallyPassing: c.InitiallyPassing(), + Check: c, }) } @@ -74,7 +96,7 @@ type GetLivenessReply struct { // GetLiveness returns a summation of the health of the node func (h *Health) GetLiveness(_ *http.Request, _ *GetLivenessArgs, reply *GetLivenessReply) error { - h.log.Debug("Health: GetLiveness called") + h.log.Info("Health: GetLiveness called") reply.Checks, reply.Healthy = h.health.Results() return nil } diff --git a/api/info/service.go b/api/info/service.go new file mode 100644 index 0000000..51324b1 --- /dev/null +++ b/api/info/service.go @@ -0,0 +1,160 @@ +// (c) 2019-2020, Ava Labs, Inc. All rights reserved. +// See the file LICENSE for licensing terms. + +package info + +import ( + "fmt" + "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 +} + +// IsBootstrappedArgs are the arguments for calling IsBootstrapped +type IsBootstrappedArgs struct { + // Alias of the chain + // Can also be the string representation of the chain's ID + Chain string `json:"chain"` +} + +// IsBootstrappedResponse are the results from calling IsBootstrapped +type IsBootstrappedResponse struct { + // True iff the chain exists and is done bootstrapping + IsBootstrapped bool `json:"isBootstrapped"` +} + +// IsBootstrapped returns nil and sets [reply.IsBootstrapped] == true iff [args.Chain] exists and is done bootstrapping +// Returns an error if the chain doesn't exist +func (service *Info) IsBootstrapped(_ *http.Request, args *IsBootstrappedArgs, reply *IsBootstrappedResponse) error { + service.log.Info("Info: IsBootstrapped called") + if args.Chain == "" { + return fmt.Errorf("argument 'chain' not given") + } + chainID, err := service.chainManager.Lookup(args.Chain) + if err != nil { + return fmt.Errorf("there is no chain with alias/ID '%s'", args.Chain) + } + reply.IsBootstrapped = service.chainManager.IsBootstrapped(chainID) + return nil +} diff --git a/api/ipcs/server.go b/api/ipcs/server.go index 30bcc5d..72a78c1 100644 --- a/api/ipcs/server.go +++ b/api/ipcs/server.go @@ -61,6 +61,7 @@ type PublishBlockchainReply struct { // PublishBlockchain publishes the finalized accepted transactions from the blockchainID over the IPC func (ipc *IPCs) PublishBlockchain(r *http.Request, args *PublishBlockchainArgs, reply *PublishBlockchainReply) error { + ipc.log.Info("IPCs: PublishBlockchain called with BlockchainID: %s", args.BlockchainID) chainID, err := ipc.chainManager.Lookup(args.BlockchainID) if err != nil { ipc.log.Error("unknown blockchainID: %s", err) @@ -116,6 +117,7 @@ type UnpublishBlockchainReply struct { // UnpublishBlockchain closes publishing of a blockchainID func (ipc *IPCs) UnpublishBlockchain(r *http.Request, args *UnpublishBlockchainArgs, reply *UnpublishBlockchainReply) error { + ipc.log.Info("IPCs: UnpublishBlockchain called with BlockchainID: %s", args.BlockchainID) chainID, err := ipc.chainManager.Lookup(args.BlockchainID) if err != nil { ipc.log.Error("unknown blockchainID %s: %s", args.BlockchainID, err) diff --git a/api/keystore/service.go b/api/keystore/service.go index 16aca06..e6979fa 100644 --- a/api/keystore/service.go +++ b/api/keystore/service.go @@ -8,29 +8,41 @@ import ( "fmt" "net/http" "sync" + "testing" "github.com/gorilla/rpc/v2" + zxcvbn "github.com/nbutton23/zxcvbn-go" + "github.com/ava-labs/gecko/chains/atomic" "github.com/ava-labs/gecko/database" "github.com/ava-labs/gecko/database/encdb" + "github.com/ava-labs/gecko/database/memdb" "github.com/ava-labs/gecko/database/prefixdb" "github.com/ava-labs/gecko/ids" "github.com/ava-labs/gecko/snow/engine/common" + "github.com/ava-labs/gecko/utils/codec" "github.com/ava-labs/gecko/utils/formatting" "github.com/ava-labs/gecko/utils/logging" - "github.com/ava-labs/gecko/vms/components/codec" jsoncodec "github.com/ava-labs/gecko/utils/json" - zxcvbn "github.com/nbutton23/zxcvbn-go" ) const ( // maxUserPassLen is the maximum length of the username or password allowed maxUserPassLen = 1024 - // requiredPassScore defines the score a password must achieve to be accepted - // as a password with strong characteristics by the zxcvbn package + // maxCheckedPassLen limits the length of the password that should be + // strength checked. + // + // As per issue https://github.com/ava-labs/gecko/issues/195 it was found + // the longer the length of password the slower zxcvbn.PasswordStrength() + // performs. To avoid performance issues, and a DoS vector, we only check + // the first 50 characters of the password. + maxCheckedPassLen = 50 + + // requiredPassScore defines the score a password must achieve to be + // accepted as a password with strong characteristics by the zxcvbn package // // The scoring mechanism defined is as follows; // @@ -135,37 +147,11 @@ func (ks *Keystore) CreateUser(_ *http.Request, args *CreateUserArgs, reply *Cre ks.lock.Lock() defer ks.lock.Unlock() - ks.log.Verbo("CreateUser called with %.*s", maxUserPassLen, args.Username) - - if len(args.Username) > maxUserPassLen || len(args.Password) > maxUserPassLen { - return errUserPassMaxLength - } - - if args.Username == "" { - return errEmptyUsername - } - if usr, err := ks.getUser(args.Username); err == nil || usr != nil { - return fmt.Errorf("user already exists: %s", args.Username) - } - - if zxcvbn.PasswordStrength(args.Password, nil).Score < requiredPassScore { - return errWeakPassword - } - - usr := &User{} - if err := usr.Initialize(args.Password); err != nil { + ks.log.Info("Keystore: CreateUser called with %.*s", maxUserPassLen, args.Username) + if err := ks.AddUser(args.Username, args.Password); err != nil { return err } - usrBytes, err := ks.codec.Marshal(usr) - if err != nil { - return err - } - - if err := ks.userDB.Put([]byte(args.Username), usrBytes); err != nil { - return err - } - ks.users[args.Username] = usr reply.Success = true return nil } @@ -183,7 +169,7 @@ func (ks *Keystore) ListUsers(_ *http.Request, args *ListUsersArgs, reply *ListU ks.lock.Lock() defer ks.lock.Unlock() - ks.log.Verbo("ListUsers called") + ks.log.Info("Keystore: ListUsers called") reply.Users = []string{} @@ -211,7 +197,7 @@ func (ks *Keystore) ExportUser(_ *http.Request, args *ExportUserArgs, reply *Exp ks.lock.Lock() defer ks.lock.Unlock() - ks.log.Verbo("ExportUser called for %s", args.Username) + ks.log.Info("Keystore: ExportUser called for %s", args.Username) usr, err := ks.getUser(args.Username) if err != nil { @@ -264,7 +250,7 @@ func (ks *Keystore) ImportUser(r *http.Request, args *ImportUserArgs, reply *Imp ks.lock.Lock() defer ks.lock.Unlock() - ks.log.Verbo("ImportUser called for %s", args.Username) + ks.log.Info("Keystore: ImportUser called for %s", args.Username) if args.Username == "" { return errEmptyUsername @@ -324,7 +310,7 @@ func (ks *Keystore) DeleteUser(_ *http.Request, args *DeleteUserArgs, reply *Del ks.lock.Lock() defer ks.lock.Unlock() - ks.log.Verbo("DeleteUser called with %s", args.Username) + ks.log.Info("Keystore: DeleteUser called with %s", args.Username) if args.Username == "" { return errEmptyUsername @@ -403,3 +389,51 @@ func (ks *Keystore) GetDatabase(bID ids.ID, username, password string) (database return encDB, nil } + +// AddUser attempts to register this username and password as a new user of the +// keystore. +func (ks *Keystore) AddUser(username, password string) error { + if len(username) > maxUserPassLen || len(password) > maxUserPassLen { + return errUserPassMaxLength + } + + if username == "" { + return errEmptyUsername + } + if usr, err := ks.getUser(username); err == nil || usr != nil { + return fmt.Errorf("user already exists: %s", username) + } + + checkPass := password + if len(password) > maxCheckedPassLen { + checkPass = password[:maxCheckedPassLen] + } + + if zxcvbn.PasswordStrength(checkPass, nil).Score < requiredPassScore { + return errWeakPassword + } + + usr := &User{} + if err := usr.Initialize(password); err != nil { + return err + } + + usrBytes, err := ks.codec.Marshal(usr) + if err != nil { + return err + } + + if err := ks.userDB.Put([]byte(username), usrBytes); err != nil { + return err + } + ks.users[username] = usr + + return nil +} + +// CreateTestKeystore returns a new keystore that can be utilized for testing +func CreateTestKeystore(t *testing.T) *Keystore { + ks := &Keystore{} + ks.Initialize(logging.NoLog{}, memdb.New()) + return ks +} diff --git a/api/keystore/service_test.go b/api/keystore/service_test.go index 9ec5cfa..3e0b18f 100644 --- a/api/keystore/service_test.go +++ b/api/keystore/service_test.go @@ -10,9 +10,7 @@ import ( "reflect" "testing" - "github.com/ava-labs/gecko/database/memdb" "github.com/ava-labs/gecko/ids" - "github.com/ava-labs/gecko/utils/logging" ) var ( @@ -22,8 +20,7 @@ var ( ) func TestServiceListNoUsers(t *testing.T) { - ks := Keystore{} - ks.Initialize(logging.NoLog{}, memdb.New()) + ks := CreateTestKeystore(t) reply := ListUsersReply{} if err := ks.ListUsers(nil, &ListUsersArgs{}, &reply); err != nil { @@ -35,8 +32,7 @@ func TestServiceListNoUsers(t *testing.T) { } func TestServiceCreateUser(t *testing.T) { - ks := Keystore{} - ks.Initialize(logging.NoLog{}, memdb.New()) + ks := CreateTestKeystore(t) { reply := CreateUserReply{} @@ -75,8 +71,7 @@ func genStr(n int) string { // TestServiceCreateUserArgsChecks generates excessively long usernames or // passwords to assure the santity checks on string length are not exceeded func TestServiceCreateUserArgsCheck(t *testing.T) { - ks := Keystore{} - ks.Initialize(logging.NoLog{}, memdb.New()) + ks := CreateTestKeystore(t) { reply := CreateUserReply{} @@ -117,8 +112,7 @@ func TestServiceCreateUserArgsCheck(t *testing.T) { // TestServiceCreateUserWeakPassword tests creating a new user with a weak // password to ensure the password strength check is working func TestServiceCreateUserWeakPassword(t *testing.T) { - ks := Keystore{} - ks.Initialize(logging.NoLog{}, memdb.New()) + ks := CreateTestKeystore(t) { reply := CreateUserReply{} @@ -138,8 +132,7 @@ func TestServiceCreateUserWeakPassword(t *testing.T) { } func TestServiceCreateDuplicate(t *testing.T) { - ks := Keystore{} - ks.Initialize(logging.NoLog{}, memdb.New()) + ks := CreateTestKeystore(t) { reply := CreateUserReply{} @@ -166,8 +159,7 @@ func TestServiceCreateDuplicate(t *testing.T) { } func TestServiceCreateUserNoName(t *testing.T) { - ks := Keystore{} - ks.Initialize(logging.NoLog{}, memdb.New()) + ks := CreateTestKeystore(t) reply := CreateUserReply{} if err := ks.CreateUser(nil, &CreateUserArgs{ @@ -178,8 +170,7 @@ func TestServiceCreateUserNoName(t *testing.T) { } func TestServiceUseBlockchainDB(t *testing.T) { - ks := Keystore{} - ks.Initialize(logging.NoLog{}, memdb.New()) + ks := CreateTestKeystore(t) { reply := CreateUserReply{} @@ -218,8 +209,7 @@ func TestServiceUseBlockchainDB(t *testing.T) { } func TestServiceExportImport(t *testing.T) { - ks := Keystore{} - ks.Initialize(logging.NoLog{}, memdb.New()) + ks := CreateTestKeystore(t) { reply := CreateUserReply{} @@ -252,8 +242,7 @@ func TestServiceExportImport(t *testing.T) { t.Fatal(err) } - newKS := Keystore{} - newKS.Initialize(logging.NoLog{}, memdb.New()) + newKS := CreateTestKeystore(t) { reply := ImportUserReply{} @@ -358,11 +347,10 @@ func TestServiceDeleteUser(t *testing.T) { for _, tt := range tests { t.Run(tt.desc, func(t *testing.T) { - ks := Keystore{} - ks.Initialize(logging.NoLog{}, memdb.New()) + ks := CreateTestKeystore(t) if tt.setup != nil { - if err := tt.setup(&ks); err != nil { + if err := tt.setup(ks); err != nil { t.Fatalf("failed to create user setup in keystore: %v", err) } } diff --git a/api/metrics/service.go b/api/metrics/service.go index 5fa9206..463456a 100644 --- a/api/metrics/service.go +++ b/api/metrics/service.go @@ -4,9 +4,10 @@ package metrics import ( - "github.com/ava-labs/gecko/snow/engine/common" "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus/promhttp" + + "github.com/ava-labs/gecko/snow/engine/common" ) // NewService returns a new prometheus service diff --git a/cache/lru_cache.go b/cache/lru_cache.go index 629d6bd..ae04138 100644 --- a/cache/lru_cache.go +++ b/cache/lru_cache.go @@ -10,6 +10,10 @@ import ( "github.com/ava-labs/gecko/ids" ) +const ( + minCacheSize = 32 +) + type entry struct { Key ids.ID Value interface{} @@ -59,7 +63,7 @@ func (c *LRU) Flush() { func (c *LRU) init() { if c.entryMap == nil { - c.entryMap = make(map[[32]byte]*list.Element) + c.entryMap = make(map[[32]byte]*list.Element, minCacheSize) } if c.entryList == nil { c.entryList = list.New() @@ -134,6 +138,6 @@ func (c *LRU) evict(key ids.ID) { func (c *LRU) flush() { c.init() - c.entryMap = make(map[[32]byte]*list.Element) + c.entryMap = make(map[[32]byte]*list.Element, minCacheSize) c.entryList = list.New() } diff --git a/cache/lru_cache_benchmark_test.go b/cache/lru_cache_benchmark_test.go new file mode 100644 index 0000000..6bdbaf8 --- /dev/null +++ b/cache/lru_cache_benchmark_test.go @@ -0,0 +1,53 @@ +package cache + +import ( + "crypto/rand" + "testing" + + "github.com/ava-labs/gecko/ids" +) + +func BenchmarkLRUCachePutSmall(b *testing.B) { + smallLen := 5 + cache := &LRU{Size: smallLen} + for n := 0; n < b.N; n++ { + for i := 0; i < smallLen; i++ { + var idBytes [32]byte + rand.Read(idBytes[:]) + cache.Put(ids.NewID(idBytes), n) + } + b.StopTimer() + cache.Flush() + b.StartTimer() + } +} + +func BenchmarkLRUCachePutMedium(b *testing.B) { + mediumLen := 250 + cache := &LRU{Size: mediumLen} + for n := 0; n < b.N; n++ { + for i := 0; i < mediumLen; i++ { + var idBytes [32]byte + rand.Read(idBytes[:]) + cache.Put(ids.NewID(idBytes), n) + } + b.StopTimer() + cache.Flush() + b.StartTimer() + } +} + +func BenchmarkLRUCachePutLarge(b *testing.B) { + largeLen := 10000 + cache := &LRU{Size: largeLen} + for n := 0; n < b.N; n++ { + for i := 0; i < largeLen; i++ { + var idBytes [32]byte + rand.Read(idBytes[:]) + cache.Put(ids.NewID(idBytes), n) + } + b.StopTimer() + cache.Flush() + b.StartTimer() + } +} diff --git a/chains/atomic/memory.go b/chains/atomic/memory.go index 448e6c9..778b9e5 100644 --- a/chains/atomic/memory.go +++ b/chains/atomic/memory.go @@ -10,9 +10,9 @@ import ( "github.com/ava-labs/gecko/database" "github.com/ava-labs/gecko/database/prefixdb" "github.com/ava-labs/gecko/ids" + "github.com/ava-labs/gecko/utils/codec" "github.com/ava-labs/gecko/utils/hashing" "github.com/ava-labs/gecko/utils/logging" - "github.com/ava-labs/gecko/vms/components/codec" ) type rcLock struct { diff --git a/chains/manager.go b/chains/manager.go index 78470b4..114cbd9 100644 --- a/chains/manager.go +++ b/chains/manager.go @@ -76,6 +76,9 @@ type Manager interface { // Add an alias to a chain Alias(ids.ID, string) error + // Returns true iff the chain with the given ID exists and is finished bootstrapping + IsBootstrapped(ids.ID) bool + Shutdown() } @@ -114,6 +117,10 @@ type manager struct { keystore *keystore.Keystore sharedMemory *atomic.SharedMemory + // Key: Chain's ID + // Value: The chain + chains map[[32]byte]*router.Handler + unblocked bool blockedChains []ChainParameters } @@ -131,7 +138,7 @@ func New( decisionEvents *triggers.EventDispatcher, consensusEvents *triggers.EventDispatcher, db database.Database, - router router.Router, + rtr router.Router, net network.Network, consensusParams avacon.Parameters, validators validators.Manager, @@ -145,7 +152,7 @@ func New( timeoutManager.Initialize(requestTimeout) go log.RecoverAndPanic(timeoutManager.Dispatch) - router.Initialize(log, &timeoutManager, gossipFrequency, shutdownTimeout) + rtr.Initialize(log, &timeoutManager, gossipFrequency, shutdownTimeout) m := &manager{ stakingEnabled: stakingEnabled, @@ -155,7 +162,7 @@ func New( decisionEvents: decisionEvents, consensusEvents: consensusEvents, db: db, - chainRouter: router, + chainRouter: rtr, net: net, timeoutManager: &timeoutManager, consensusParams: consensusParams, @@ -165,6 +172,7 @@ func New( server: server, keystore: keystore, sharedMemory: sharedMemory, + chains: make(map[[32]byte]*router.Handler), } m.Initialize() return m @@ -454,7 +462,7 @@ func (m *manager) createAvalancheChain( eng: &engine, }) } - + m.chains[ctx.ChainID.Key()] = handler return nil } @@ -546,9 +554,20 @@ func (m *manager) createSnowmanChain( eng: &engine, }) } + m.chains[ctx.ChainID.Key()] = handler return nil } +func (m *manager) IsBootstrapped(id ids.ID) bool { + chain, exists := m.chains[id.Key()] + if !exists { + return false + } + chain.Context().Lock.Lock() + defer chain.Context().Lock.Unlock() + return chain.Engine().IsBootstrapped() +} + // Shutdown stops all the chains func (m *manager) Shutdown() { m.chainRouter.Shutdown() } diff --git a/chains/mock_manager.go b/chains/mock_manager.go index 7c0f86b..33c5535 100644 --- a/chains/mock_manager.go +++ b/chains/mock_manager.go @@ -35,3 +35,6 @@ func (mm MockManager) Alias(ids.ID, string) error { return nil } // Shutdown ... func (mm MockManager) Shutdown() {} + +// IsBootstrapped ... +func (mm MockManager) IsBootstrapped(ids.ID) bool { return false } diff --git a/database/common.go b/database/common.go new file mode 100644 index 0000000..26b0531 --- /dev/null +++ b/database/common.go @@ -0,0 +1,14 @@ +package database + +const ( + // MaxExcessCapacityFactor ... + // If, when a batch is reset, the cap(batch)/len(batch) > MaxExcessCapacityFactor, + // the underlying array's capacity will be reduced by a factor of capacityReductionFactor. + // Higher value for MaxExcessCapacityFactor --> less aggressive array downsizing --> less memory allocations + // but more unnecessary data in the underlying array that can't be garbage collected. + // Higher value for CapacityReductionFactor --> more aggressive array downsizing --> more memory allocations + // but less unnecessary data in the underlying array that can't be garbage collected. + MaxExcessCapacityFactor = 4 + // CapacityReductionFactor ... + CapacityReductionFactor = 2 +) diff --git a/database/encdb/db.go b/database/encdb/db.go index eb06549..8f0d8e3 100644 --- a/database/encdb/db.go +++ b/database/encdb/db.go @@ -13,8 +13,8 @@ import ( "github.com/ava-labs/gecko/database" "github.com/ava-labs/gecko/database/nodb" "github.com/ava-labs/gecko/utils" + "github.com/ava-labs/gecko/utils/codec" "github.com/ava-labs/gecko/utils/hashing" - "github.com/ava-labs/gecko/vms/components/codec" ) // Database encrypts all values that are provided @@ -201,7 +201,11 @@ func (b *batch) Write() error { // Reset resets the batch for reuse. func (b *batch) Reset() { - b.writes = b.writes[:0] + if cap(b.writes) > len(b.writes)*database.MaxExcessCapacityFactor { + b.writes = make([]keyValue, 0, cap(b.writes)/database.CapacityReductionFactor) + } else { + b.writes = b.writes[:0] + } b.Batch.Reset() } diff --git a/database/leveldb/db.go b/database/leveldb/db.go index edcb4be..7055d61 100644 --- a/database/leveldb/db.go +++ b/database/leveldb/db.go @@ -6,14 +6,15 @@ package leveldb import ( "bytes" - "github.com/ava-labs/gecko/database" - "github.com/ava-labs/gecko/utils" "github.com/syndtr/goleveldb/leveldb" "github.com/syndtr/goleveldb/leveldb/errors" "github.com/syndtr/goleveldb/leveldb/filter" "github.com/syndtr/goleveldb/leveldb/iterator" "github.com/syndtr/goleveldb/leveldb/opt" "github.com/syndtr/goleveldb/leveldb/util" + + "github.com/ava-labs/gecko/database" + "github.com/ava-labs/gecko/utils" ) const ( diff --git a/database/memdb/db.go b/database/memdb/db.go index de0cae3..94ba395 100644 --- a/database/memdb/db.go +++ b/database/memdb/db.go @@ -13,8 +13,10 @@ import ( "github.com/ava-labs/gecko/utils" ) -// DefaultSize is the default initial size of the memory database -const DefaultSize = 1 << 10 +const ( + // DefaultSize is the default initial size of the memory database + DefaultSize = 1 << 10 +) // Database is an ephemeral key-value store that implements the Database // interface. @@ -191,7 +193,11 @@ func (b *batch) Write() error { // Reset implements the Batch interface func (b *batch) Reset() { - b.writes = b.writes[:0] + if cap(b.writes) > len(b.writes)*database.MaxExcessCapacityFactor { + b.writes = make([]keyValue, 0, cap(b.writes)/database.CapacityReductionFactor) + } else { + b.writes = b.writes[:0] + } b.size = 0 } diff --git a/database/nodb/db.go b/database/nodb/db.go index 3f1bceb..9a7525f 100644 --- a/database/nodb/db.go +++ b/database/nodb/db.go @@ -17,7 +17,7 @@ func (*Database) Has([]byte) (bool, error) { return false, database.ErrClosed } func (*Database) Get([]byte) ([]byte, error) { return nil, database.ErrClosed } // Put returns nil -func (*Database) Put(_ []byte, _ []byte) error { return database.ErrClosed } +func (*Database) Put(_, _ []byte) error { return database.ErrClosed } // Delete returns nil func (*Database) Delete([]byte) error { return database.ErrClosed } diff --git a/database/prefixdb/db.go b/database/prefixdb/db.go index 34bc50d..7f606b2 100644 --- a/database/prefixdb/db.go +++ b/database/prefixdb/db.go @@ -199,7 +199,11 @@ func (b *batch) Write() error { // Reset resets the batch for reuse. func (b *batch) Reset() { - b.writes = b.writes[:0] + if cap(b.writes) > len(b.writes)*database.MaxExcessCapacityFactor { + b.writes = make([]keyValue, 0, cap(b.writes)/database.CapacityReductionFactor) + } else { + b.writes = b.writes[:0] + } b.Batch.Reset() } diff --git a/database/rpcdb/db_client.go b/database/rpcdb/db_client.go index 67af7ef..401e404 100644 --- a/database/rpcdb/db_client.go +++ b/database/rpcdb/db_client.go @@ -27,7 +27,7 @@ func NewClient(client rpcdbproto.DatabaseClient) *DatabaseClient { return &DatabaseClient{client: client} } -// Has returns false, nil +// Has attempts to return if the database has a key with the provided value. func (db *DatabaseClient) Has(key []byte) (bool, error) { resp, err := db.client.Has(context.Background(), &rpcdbproto.HasRequest{ Key: key, @@ -38,7 +38,7 @@ func (db *DatabaseClient) Has(key []byte) (bool, error) { return resp.Has, nil } -// Get returns nil, error +// Get attempts to return the value that was mapped to the key that was provided func (db *DatabaseClient) Get(key []byte) ([]byte, error) { resp, err := db.client.Get(context.Background(), &rpcdbproto.GetRequest{ Key: key, @@ -49,7 +49,7 @@ func (db *DatabaseClient) Get(key []byte) ([]byte, error) { return resp.Value, nil } -// Put returns nil +// Put attempts to set the value this key maps to func (db *DatabaseClient) Put(key, value []byte) error { _, err := db.client.Put(context.Background(), &rpcdbproto.PutRequest{ Key: key, @@ -58,7 +58,7 @@ func (db *DatabaseClient) Put(key, value []byte) error { return updateError(err) } -// Delete returns nil +// Delete attempts to remove any mapping from the key func (db *DatabaseClient) Delete(key []byte) error { _, err := db.client.Delete(context.Background(), &rpcdbproto.DeleteRequest{ Key: key, @@ -99,7 +99,7 @@ func (db *DatabaseClient) NewIteratorWithStartAndPrefix(start, prefix []byte) da } } -// Stat returns an error +// Stat attempts to return the statistic of this database func (db *DatabaseClient) Stat(property string) (string, error) { resp, err := db.client.Stat(context.Background(), &rpcdbproto.StatRequest{ Property: property, @@ -110,7 +110,7 @@ func (db *DatabaseClient) Stat(property string) (string, error) { return resp.Stat, nil } -// Compact returns nil +// Compact attempts to optimize the space utilization in the provided range func (db *DatabaseClient) Compact(start, limit []byte) error { _, err := db.client.Compact(context.Background(), &rpcdbproto.CompactRequest{ Start: start, @@ -119,7 +119,7 @@ func (db *DatabaseClient) Compact(start, limit []byte) error { return updateError(err) } -// Close returns nil +// Close attempts to close the database func (db *DatabaseClient) Close() error { _, err := db.client.Close(context.Background(), &rpcdbproto.CloseRequest{}) return updateError(err) @@ -180,7 +180,11 @@ func (b *batch) Write() error { } func (b *batch) Reset() { - b.writes = b.writes[:0] + if cap(b.writes) > len(b.writes)*database.MaxExcessCapacityFactor { + b.writes = make([]keyValue, 0, cap(b.writes)/database.CapacityReductionFactor) + } else { + b.writes = b.writes[:0] + } b.size = 0 } @@ -207,7 +211,8 @@ type iterator struct { err error } -// Next returns false +// Next attempts to move the iterator to the next element and returns if this +// succeeded func (it *iterator) Next() bool { resp, err := it.db.client.IteratorNext(context.Background(), &rpcdbproto.IteratorNextRequest{ Id: it.id, @@ -221,7 +226,7 @@ func (it *iterator) Next() bool { return resp.FoundNext } -// Error returns any errors +// Error returns any that occurred while iterating func (it *iterator) Error() error { if it.err != nil { return it.err @@ -234,19 +239,21 @@ func (it *iterator) Error() error { return it.err } -// Key returns nil +// Key returns the key of the current element func (it *iterator) Key() []byte { return it.key } -// Value returns nil +// Value returns the value of the current element func (it *iterator) Value() []byte { return it.value } -// Release does nothing +// Release frees any resources held by the iterator func (it *iterator) Release() { it.db.client.IteratorRelease(context.Background(), &rpcdbproto.IteratorReleaseRequest{ Id: it.id, }) } +// updateError sets the error value to the errors required by the Database +// interface func updateError(err error) error { if err == nil { return nil diff --git a/database/rpcdb/db_server.go b/database/rpcdb/db_server.go index fe4b47a..cc32db8 100644 --- a/database/rpcdb/db_server.go +++ b/database/rpcdb/db_server.go @@ -34,16 +34,16 @@ func NewServer(db database.Database) *DatabaseServer { } } -// Has ... +// Has delegates the Has call to the managed database and returns the result func (db *DatabaseServer) Has(_ context.Context, req *rpcdbproto.HasRequest) (*rpcdbproto.HasResponse, error) { has, err := db.db.Has(req.Key) if err != nil { return nil, err } - return &rpcdbproto.HasResponse{Has: has}, nil + return &rpcdbproto.HasResponse{Has: has}, err } -// Get ... +// Get delegates the Get call to the managed database and returns the result func (db *DatabaseServer) Get(_ context.Context, req *rpcdbproto.GetRequest) (*rpcdbproto.GetResponse, error) { value, err := db.db.Get(req.Key) if err != nil { @@ -52,17 +52,18 @@ func (db *DatabaseServer) Get(_ context.Context, req *rpcdbproto.GetRequest) (*r return &rpcdbproto.GetResponse{Value: value}, nil } -// Put ... +// Put delegates the Put call to the managed database and returns the result func (db *DatabaseServer) Put(_ context.Context, req *rpcdbproto.PutRequest) (*rpcdbproto.PutResponse, error) { return &rpcdbproto.PutResponse{}, db.db.Put(req.Key, req.Value) } -// Delete ... +// Delete delegates the Delete call to the managed database and returns the +// result func (db *DatabaseServer) Delete(_ context.Context, req *rpcdbproto.DeleteRequest) (*rpcdbproto.DeleteResponse, error) { return &rpcdbproto.DeleteResponse{}, db.db.Delete(req.Key) } -// Stat ... +// Stat delegates the Stat call to the managed database and returns the result func (db *DatabaseServer) Stat(_ context.Context, req *rpcdbproto.StatRequest) (*rpcdbproto.StatResponse, error) { stat, err := db.db.Stat(req.Property) if err != nil { @@ -71,17 +72,19 @@ func (db *DatabaseServer) Stat(_ context.Context, req *rpcdbproto.StatRequest) ( return &rpcdbproto.StatResponse{Stat: stat}, nil } -// Compact ... +// Compact delegates the Compact call to the managed database and returns the +// result func (db *DatabaseServer) Compact(_ context.Context, req *rpcdbproto.CompactRequest) (*rpcdbproto.CompactResponse, error) { return &rpcdbproto.CompactResponse{}, db.db.Compact(req.Start, req.Limit) } -// Close ... -func (db *DatabaseServer) Close(_ context.Context, _ *rpcdbproto.CloseRequest) (*rpcdbproto.CloseResponse, error) { +// Close delegates the Close call to the managed database and returns the result +func (db *DatabaseServer) Close(context.Context, *rpcdbproto.CloseRequest) (*rpcdbproto.CloseResponse, error) { return &rpcdbproto.CloseResponse{}, db.db.Close() } -// WriteBatch ... +// WriteBatch takes in a set of key-value pairs and atomically writes them to +// the internal database func (db *DatabaseServer) WriteBatch(_ context.Context, req *rpcdbproto.WriteBatchRequest) (*rpcdbproto.WriteBatchResponse, error) { db.batch.Reset() @@ -100,7 +103,8 @@ func (db *DatabaseServer) WriteBatch(_ context.Context, req *rpcdbproto.WriteBat return &rpcdbproto.WriteBatchResponse{}, db.batch.Write() } -// NewIteratorWithStartAndPrefix ... +// NewIteratorWithStartAndPrefix allocates an iterator and returns the iterator +// ID func (db *DatabaseServer) NewIteratorWithStartAndPrefix(_ context.Context, req *rpcdbproto.NewIteratorWithStartAndPrefixRequest) (*rpcdbproto.NewIteratorWithStartAndPrefixResponse, error) { id := db.nextIteratorID it := db.db.NewIteratorWithStartAndPrefix(req.Start, req.Prefix) @@ -110,7 +114,7 @@ func (db *DatabaseServer) NewIteratorWithStartAndPrefix(_ context.Context, req * return &rpcdbproto.NewIteratorWithStartAndPrefixResponse{Id: id}, nil } -// IteratorNext ... +// IteratorNext attempts to call next on the requested iterator func (db *DatabaseServer) IteratorNext(_ context.Context, req *rpcdbproto.IteratorNextRequest) (*rpcdbproto.IteratorNextResponse, error) { it, exists := db.iterators[req.Id] if !exists { @@ -123,7 +127,7 @@ func (db *DatabaseServer) IteratorNext(_ context.Context, req *rpcdbproto.Iterat }, nil } -// IteratorError ... +// IteratorError attempts to report any errors that occurred during iteration func (db *DatabaseServer) IteratorError(_ context.Context, req *rpcdbproto.IteratorErrorRequest) (*rpcdbproto.IteratorErrorResponse, error) { it, exists := db.iterators[req.Id] if !exists { @@ -132,7 +136,7 @@ func (db *DatabaseServer) IteratorError(_ context.Context, req *rpcdbproto.Itera return &rpcdbproto.IteratorErrorResponse{}, it.Error() } -// IteratorRelease ... +// IteratorRelease attempts to release the resources allocated to an iterator func (db *DatabaseServer) IteratorRelease(_ context.Context, req *rpcdbproto.IteratorReleaseRequest) (*rpcdbproto.IteratorReleaseResponse, error) { it, exists := db.iterators[req.Id] if exists { diff --git a/database/versiondb/db.go b/database/versiondb/db.go index fb692bf..a1f9a18 100644 --- a/database/versiondb/db.go +++ b/database/versiondb/db.go @@ -18,9 +18,10 @@ import ( // database, writing changes to the underlying database only when commit is // called. type Database struct { - lock sync.RWMutex - mem map[string]valueDelete - db database.Database + lock sync.RWMutex + mem map[string]valueDelete + db database.Database + batch database.Batch } type valueDelete struct { @@ -31,8 +32,9 @@ type valueDelete struct { // New returns a new prefixed database func New(db database.Database) *Database { return &Database{ - mem: make(map[string]valueDelete, memdb.DefaultSize), - db: db, + mem: make(map[string]valueDelete, memdb.DefaultSize), + db: db, + batch: db.NewBatch(), } } @@ -169,6 +171,7 @@ func (db *Database) SetDatabase(newDB database.Database) error { } db.db = newDB + db.batch = newDB.NewBatch() return nil } @@ -192,6 +195,7 @@ func (db *Database) Commit() error { if err := batch.Write(); err != nil { return err } + batch.Reset() db.abort() return nil } @@ -206,7 +210,10 @@ func (db *Database) Abort() { func (db *Database) abort() { db.mem = make(map[string]valueDelete, memdb.DefaultSize) } -// CommitBatch returns a batch that will commit all pending writes to the underlying database +// CommitBatch returns a batch that contains all uncommitted puts/deletes. +// Calling Write() on the returned batch causes the puts/deletes to be +// written to the underlying database. The returned batch should be written before +// future calls to this DB unless the batch will never be written. func (db *Database) CommitBatch() (database.Batch, error) { db.lock.Lock() defer db.lock.Unlock() @@ -214,26 +221,25 @@ func (db *Database) CommitBatch() (database.Batch, error) { return db.commitBatch() } +// Put all of the puts/deletes in memory into db.batch +// and return the batch func (db *Database) commitBatch() (database.Batch, error) { if db.mem == nil { return nil, database.ErrClosed } - batch := db.db.NewBatch() + db.batch.Reset() for key, value := range db.mem { if value.delete { - if err := batch.Delete([]byte(key)); err != nil { + if err := db.batch.Delete([]byte(key)); err != nil { return nil, err } - } else if err := batch.Put([]byte(key), value.value); err != nil { + } else if err := db.batch.Put([]byte(key), value.value); err != nil { return nil, err } } - if err := batch.Write(); err != nil { - return nil, err - } - return batch, nil + return db.batch, nil } // Close implements the database.Database interface @@ -244,6 +250,7 @@ func (db *Database) Close() error { if db.mem == nil { return database.ErrClosed } + db.batch = nil db.mem = nil db.db = nil return nil @@ -298,7 +305,11 @@ func (b *batch) Write() error { // Reset implements the Database interface func (b *batch) Reset() { - b.writes = b.writes[:0] + if cap(b.writes) > len(b.writes)*database.MaxExcessCapacityFactor { + b.writes = make([]keyValue, 0, cap(b.writes)/database.CapacityReductionFactor) + } else { + b.writes = b.writes[:0] + } b.size = 0 } diff --git a/database/versiondb/db_test.go b/database/versiondb/db_test.go index 70cf8ff..345655a 100644 --- a/database/versiondb/db_test.go +++ b/database/versiondb/db_test.go @@ -299,6 +299,10 @@ func TestCommitBatch(t *testing.T) { if err := db.Put(key1, value1); err != nil { t.Fatalf("Unexpected error on db.Put: %s", err) + } else if has, err := baseDB.Has(key1); err != nil { + t.Fatalf("Unexpected error on db.Has: %s", err) + } else if has { + t.Fatalf("Unexpected result of db.Has: %v", has) } batch, err := db.CommitBatch() @@ -307,7 +311,11 @@ func TestCommitBatch(t *testing.T) { } db.Abort() - if err := batch.Write(); err != nil { + if has, err := db.Has(key1); err != nil { + t.Fatalf("Unexpected error on db.Has: %s", err) + } else if has { + t.Fatalf("Unexpected result of db.Has: %v", has) + } else if err := batch.Write(); err != nil { t.Fatalf("Unexpected error on batch.Write: %s", err) } diff --git a/genesis/aliases.go b/genesis/aliases.go index 80e5dcd..b1eae46 100644 --- a/genesis/aliases.go +++ b/genesis/aliases.go @@ -18,27 +18,27 @@ import ( // Aliases returns the default aliases based on the network ID func Aliases(networkID uint32) (map[string][]string, map[[32]byte][]string, map[[32]byte][]string, error) { generalAliases := map[string][]string{ - "vm/" + platformvm.ID.String(): []string{"vm/platform"}, - "vm/" + avm.ID.String(): []string{"vm/avm"}, - "vm/" + EVMID.String(): []string{"vm/evm"}, - "vm/" + spdagvm.ID.String(): []string{"vm/spdag"}, - "vm/" + spchainvm.ID.String(): []string{"vm/spchain"}, - "vm/" + timestampvm.ID.String(): []string{"vm/timestamp"}, - "bc/" + ids.Empty.String(): []string{"P", "platform", "bc/P", "bc/platform"}, + "vm/" + platformvm.ID.String(): {"vm/platform"}, + "vm/" + avm.ID.String(): {"vm/avm"}, + "vm/" + EVMID.String(): {"vm/evm"}, + "vm/" + spdagvm.ID.String(): {"vm/spdag"}, + "vm/" + spchainvm.ID.String(): {"vm/spchain"}, + "vm/" + timestampvm.ID.String(): {"vm/timestamp"}, + "bc/" + ids.Empty.String(): {"P", "platform", "bc/P", "bc/platform"}, } chainAliases := map[[32]byte][]string{ - ids.Empty.Key(): []string{"P", "platform"}, + ids.Empty.Key(): {"P", "platform"}, } vmAliases := map[[32]byte][]string{ - platformvm.ID.Key(): []string{"platform"}, - avm.ID.Key(): []string{"avm"}, - EVMID.Key(): []string{"evm"}, - spdagvm.ID.Key(): []string{"spdag"}, - spchainvm.ID.Key(): []string{"spchain"}, - timestampvm.ID.Key(): []string{"timestamp"}, - secp256k1fx.ID.Key(): []string{"secp256k1fx"}, - nftfx.ID.Key(): []string{"nftfx"}, - propertyfx.ID.Key(): []string{"propertyfx"}, + platformvm.ID.Key(): {"platform"}, + avm.ID.Key(): {"avm"}, + EVMID.Key(): {"evm"}, + spdagvm.ID.Key(): {"spdag"}, + spchainvm.ID.Key(): {"spchain"}, + timestampvm.ID.Key(): {"timestamp"}, + secp256k1fx.ID.Key(): {"secp256k1fx"}, + nftfx.ID.Key(): {"nftfx"}, + propertyfx.ID.Key(): {"propertyfx"}, } genesisBytes, err := Genesis(networkID) diff --git a/genesis/config.go b/genesis/config.go index 7442ca0..832d694 100644 --- a/genesis/config.go +++ b/genesis/config.go @@ -50,6 +50,122 @@ func (c *Config) init() error { // Hard coded genesis constants var ( + EverestConfig = Config{ + MintAddresses: []string{ + "95YUFjhDG892VePMzpwKF9JzewGKvGRi3", + }, + FundedAddresses: []string{ + "9uKvvA7E35QCwLvAaohXTCfFejbf3Rv17", + "JLrYNMYXANGj43BfWXBxMMAEenUBp1Sbn", + "7TUTzwrU6nbZtWHjTHEpdneUvjKBxb3EM", + "77mPUXBdQKwQpPoX6rckCZGLGGdkuG1G6", + "4gGWdFZ4Gax1B466YKXyKRRpWLb42Afdt", + "CKTkzAPsRxCreyiDTnjGxLmjMarxF28fi", + "4ABm9gFHVtsNdcKSd1xsacFkGneSgzpaa", + "DpL8PTsrjtLzv5J8LL3D2A6YcnCTqrNH9", + "ZdhZv6oZrmXLyFDy6ovXAu6VxmbTsT2h", + "6cesTteH62Y5mLoDBUASaBvCXuL2AthL", + }, + StakerIDs: []string{ + "LQwRLm4cbJ7T2kxcxp4uXCU5XD8DFrE1C", + "hArafGhY2HFTbwaaVh1CSCUCUCiJ2Vfb", + "2m38qc95mhHXtrhjyGbe7r2NhniqHHJRB", + "4QBwET5o8kUhvt9xArhir4d3R25CtmZho", + "NpagUxt6KQiwPch9Sd4osv8kD1TZnkjdk", + }, + EVMBytes: []byte{ + 0x7b, 0x22, 0x63, 0x6f, 0x6e, 0x66, 0x69, 0x67, + 0x22, 0x3a, 0x7b, 0x22, 0x63, 0x68, 0x61, 0x69, + 0x6e, 0x49, 0x64, 0x22, 0x3a, 0x34, 0x33, 0x31, + 0x31, 0x30, 0x2c, 0x22, 0x68, 0x6f, 0x6d, 0x65, + 0x73, 0x74, 0x65, 0x61, 0x64, 0x42, 0x6c, 0x6f, + 0x63, 0x6b, 0x22, 0x3a, 0x30, 0x2c, 0x22, 0x64, + 0x61, 0x6f, 0x46, 0x6f, 0x72, 0x6b, 0x42, 0x6c, + 0x6f, 0x63, 0x6b, 0x22, 0x3a, 0x30, 0x2c, 0x22, + 0x64, 0x61, 0x6f, 0x46, 0x6f, 0x72, 0x6b, 0x53, + 0x75, 0x70, 0x70, 0x6f, 0x72, 0x74, 0x22, 0x3a, + 0x74, 0x72, 0x75, 0x65, 0x2c, 0x22, 0x65, 0x69, + 0x70, 0x31, 0x35, 0x30, 0x42, 0x6c, 0x6f, 0x63, + 0x6b, 0x22, 0x3a, 0x30, 0x2c, 0x22, 0x65, 0x69, + 0x70, 0x31, 0x35, 0x30, 0x48, 0x61, 0x73, 0x68, + 0x22, 0x3a, 0x22, 0x30, 0x78, 0x32, 0x30, 0x38, + 0x36, 0x37, 0x39, 0x39, 0x61, 0x65, 0x65, 0x62, + 0x65, 0x61, 0x65, 0x31, 0x33, 0x35, 0x63, 0x32, + 0x34, 0x36, 0x63, 0x36, 0x35, 0x30, 0x32, 0x31, + 0x63, 0x38, 0x32, 0x62, 0x34, 0x65, 0x31, 0x35, + 0x61, 0x32, 0x63, 0x34, 0x35, 0x31, 0x33, 0x34, + 0x30, 0x39, 0x39, 0x33, 0x61, 0x61, 0x63, 0x66, + 0x64, 0x32, 0x37, 0x35, 0x31, 0x38, 0x38, 0x36, + 0x35, 0x31, 0x34, 0x66, 0x30, 0x22, 0x2c, 0x22, + 0x65, 0x69, 0x70, 0x31, 0x35, 0x35, 0x42, 0x6c, + 0x6f, 0x63, 0x6b, 0x22, 0x3a, 0x30, 0x2c, 0x22, + 0x65, 0x69, 0x70, 0x31, 0x35, 0x38, 0x42, 0x6c, + 0x6f, 0x63, 0x6b, 0x22, 0x3a, 0x30, 0x2c, 0x22, + 0x62, 0x79, 0x7a, 0x61, 0x6e, 0x74, 0x69, 0x75, + 0x6d, 0x42, 0x6c, 0x6f, 0x63, 0x6b, 0x22, 0x3a, + 0x30, 0x2c, 0x22, 0x63, 0x6f, 0x6e, 0x73, 0x74, + 0x61, 0x6e, 0x74, 0x69, 0x6e, 0x6f, 0x70, 0x6c, + 0x65, 0x42, 0x6c, 0x6f, 0x63, 0x6b, 0x22, 0x3a, + 0x30, 0x2c, 0x22, 0x70, 0x65, 0x74, 0x65, 0x72, + 0x73, 0x62, 0x75, 0x72, 0x67, 0x42, 0x6c, 0x6f, + 0x63, 0x6b, 0x22, 0x3a, 0x30, 0x7d, 0x2c, 0x22, + 0x6e, 0x6f, 0x6e, 0x63, 0x65, 0x22, 0x3a, 0x22, + 0x30, 0x78, 0x30, 0x22, 0x2c, 0x22, 0x74, 0x69, + 0x6d, 0x65, 0x73, 0x74, 0x61, 0x6d, 0x70, 0x22, + 0x3a, 0x22, 0x30, 0x78, 0x30, 0x22, 0x2c, 0x22, + 0x65, 0x78, 0x74, 0x72, 0x61, 0x44, 0x61, 0x74, + 0x61, 0x22, 0x3a, 0x22, 0x30, 0x78, 0x30, 0x30, + 0x22, 0x2c, 0x22, 0x67, 0x61, 0x73, 0x4c, 0x69, + 0x6d, 0x69, 0x74, 0x22, 0x3a, 0x22, 0x30, 0x78, + 0x35, 0x66, 0x35, 0x65, 0x31, 0x30, 0x30, 0x22, + 0x2c, 0x22, 0x64, 0x69, 0x66, 0x66, 0x69, 0x63, + 0x75, 0x6c, 0x74, 0x79, 0x22, 0x3a, 0x22, 0x30, + 0x78, 0x30, 0x22, 0x2c, 0x22, 0x6d, 0x69, 0x78, + 0x48, 0x61, 0x73, 0x68, 0x22, 0x3a, 0x22, 0x30, + 0x78, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, + 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, + 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, + 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, + 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, + 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, + 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, + 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, + 0x30, 0x22, 0x2c, 0x22, 0x63, 0x6f, 0x69, 0x6e, + 0x62, 0x61, 0x73, 0x65, 0x22, 0x3a, 0x22, 0x30, + 0x78, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, + 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, + 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, + 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, + 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, + 0x30, 0x22, 0x2c, 0x22, 0x61, 0x6c, 0x6c, 0x6f, + 0x63, 0x22, 0x3a, 0x7b, 0x22, 0x35, 0x37, 0x32, + 0x66, 0x34, 0x64, 0x38, 0x30, 0x66, 0x31, 0x30, + 0x66, 0x36, 0x36, 0x33, 0x62, 0x35, 0x30, 0x34, + 0x39, 0x66, 0x37, 0x38, 0x39, 0x35, 0x34, 0x36, + 0x66, 0x32, 0x35, 0x66, 0x37, 0x30, 0x62, 0x62, + 0x36, 0x32, 0x61, 0x37, 0x66, 0x22, 0x3a, 0x7b, + 0x22, 0x62, 0x61, 0x6c, 0x61, 0x6e, 0x63, 0x65, + 0x22, 0x3a, 0x22, 0x30, 0x78, 0x33, 0x33, 0x62, + 0x32, 0x65, 0x33, 0x63, 0x39, 0x66, 0x64, 0x30, + 0x38, 0x30, 0x34, 0x30, 0x30, 0x30, 0x30, 0x30, + 0x30, 0x30, 0x30, 0x30, 0x22, 0x7d, 0x7d, 0x2c, + 0x22, 0x6e, 0x75, 0x6d, 0x62, 0x65, 0x72, 0x22, + 0x3a, 0x22, 0x30, 0x78, 0x30, 0x22, 0x2c, 0x22, + 0x67, 0x61, 0x73, 0x55, 0x73, 0x65, 0x64, 0x22, + 0x3a, 0x22, 0x30, 0x78, 0x30, 0x22, 0x2c, 0x22, + 0x70, 0x61, 0x72, 0x65, 0x6e, 0x74, 0x48, 0x61, + 0x73, 0x68, 0x22, 0x3a, 0x22, 0x30, 0x78, 0x30, + 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, + 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, + 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, + 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, + 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, + 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, + 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, + 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x22, + 0x7d, + }, + } DenaliConfig = Config{ MintAddresses: []string{ "95YUFjhDG892VePMzpwKF9JzewGKvGRi3", @@ -393,6 +509,8 @@ var ( // GetConfig ... func GetConfig(networkID uint32) *Config { switch networkID { + case EverestID: + return &EverestConfig case DenaliID: return &DenaliConfig case CascadeID: diff --git a/genesis/genesis.go b/genesis/genesis.go index 4cad047..f37a584 100644 --- a/genesis/genesis.go +++ b/genesis/genesis.go @@ -9,12 +9,12 @@ import ( "time" "github.com/ava-labs/gecko/ids" + "github.com/ava-labs/gecko/utils/codec" "github.com/ava-labs/gecko/utils/formatting" "github.com/ava-labs/gecko/utils/json" "github.com/ava-labs/gecko/utils/units" "github.com/ava-labs/gecko/utils/wrappers" "github.com/ava-labs/gecko/vms/avm" - "github.com/ava-labs/gecko/vms/components/codec" "github.com/ava-labs/gecko/vms/nftfx" "github.com/ava-labs/gecko/vms/platformvm" "github.com/ava-labs/gecko/vms/propertyfx" @@ -156,7 +156,7 @@ func FromConfig(networkID uint32, config *Config) ([]byte, error) { // Specify the chains that exist upon this network's creation platformvmArgs.Chains = []platformvm.APIChain{ - platformvm.APIChain{ + { GenesisData: avmReply.Bytes, SubnetID: platformvm.DefaultSubnetID, VMID: avm.ID, @@ -167,25 +167,25 @@ func FromConfig(networkID uint32, config *Config) ([]byte, error) { }, Name: "X-Chain", }, - platformvm.APIChain{ + { GenesisData: formatting.CB58{Bytes: config.EVMBytes}, SubnetID: platformvm.DefaultSubnetID, VMID: EVMID, Name: "C-Chain", }, - platformvm.APIChain{ + { GenesisData: spdagvmReply.Bytes, SubnetID: platformvm.DefaultSubnetID, VMID: spdagvm.ID, Name: "Simple DAG Payments", }, - platformvm.APIChain{ + { GenesisData: spchainvmReply.Bytes, SubnetID: platformvm.DefaultSubnetID, VMID: spchainvm.ID, Name: "Simple Chain Payments", }, - platformvm.APIChain{ + { GenesisData: formatting.CB58{Bytes: []byte{}}, // There is no genesis data SubnetID: platformvm.DefaultSubnetID, VMID: timestampvm.ID, diff --git a/genesis/genesis_test.go b/genesis/genesis_test.go index 63742a7..292fdee 100644 --- a/genesis/genesis_test.go +++ b/genesis/genesis_test.go @@ -23,7 +23,10 @@ func TestNetworkName(t *testing.T) { if name := NetworkName(DenaliID); name != DenaliName { t.Fatalf("NetworkID was incorrectly named. Result: %s ; Expected: %s", name, DenaliName) } - if name := NetworkName(TestnetID); name != DenaliName { + if name := NetworkName(EverestID); name != EverestName { + t.Fatalf("NetworkID was incorrectly named. Result: %s ; Expected: %s", name, EverestName) + } + if name := NetworkName(DenaliID); name != DenaliName { t.Fatalf("NetworkID was incorrectly named. Result: %s ; Expected: %s", name, DenaliName) } if name := NetworkName(4294967295); name != "network-4294967295" { diff --git a/genesis/network_id.go b/genesis/network_id.go index 7be7968..880583e 100644 --- a/genesis/network_id.go +++ b/genesis/network_id.go @@ -16,6 +16,7 @@ var ( MainnetID uint32 = 1 CascadeID uint32 = 2 DenaliID uint32 = 3 + EverestID uint32 = 4 TestnetID uint32 = 3 LocalID uint32 = 12345 @@ -23,6 +24,7 @@ var ( MainnetName = "mainnet" CascadeName = "cascade" DenaliName = "denali" + EverestName = "everest" TestnetName = "testnet" LocalName = "local" @@ -31,6 +33,7 @@ var ( MainnetID: MainnetName, CascadeID: CascadeName, DenaliID: DenaliName, + EverestID: EverestName, LocalID: LocalName, } @@ -38,6 +41,7 @@ var ( MainnetName: MainnetID, CascadeName: CascadeID, DenaliName: DenaliID, + EverestName: EverestID, TestnetName: TestnetID, LocalName: LocalID, diff --git a/go.mod b/go.mod index 4636c8c..ea394f4 100644 --- a/go.mod +++ b/go.mod @@ -6,10 +6,10 @@ require ( github.com/AppsFlyer/go-sundheit v0.2.0 github.com/allegro/bigcache v1.2.1 // indirect github.com/aristanetworks/goarista v0.0.0-20200520141224-0f14e646773f // indirect - github.com/ava-labs/coreth v0.2.4 // Added manually; don't delete + github.com/ava-labs/coreth v0.2.5 // indirect; Added manually; don't delete github.com/ava-labs/go-ethereum v1.9.3 // indirect github.com/deckarep/golang-set v1.7.1 // indirect - github.com/decred/dcrd/dcrec/secp256k1 v1.0.3 + github.com/decred/dcrd/dcrec/secp256k1 v1.0.3 // indirect github.com/decred/dcrd/dcrec/secp256k1/v3 v3.0.0-20200526030155-0c6c7ca85d3b github.com/edsrzf/mmap-go v1.0.0 // indirect github.com/elastic/gosigar v0.10.5 // indirect @@ -20,6 +20,7 @@ require ( github.com/gorilla/mux v1.7.4 github.com/gorilla/rpc v1.2.0 github.com/gorilla/websocket v1.4.2 + github.com/hashicorp/go-hclog v0.0.0-20180709165350-ff2cf002a8dd github.com/hashicorp/go-plugin v1.3.0 github.com/hashicorp/golang-lru v0.5.4 // indirect github.com/huin/goupnp v1.0.0 @@ -28,7 +29,7 @@ require ( github.com/karalabe/usb v0.0.0-20191104083709-911d15fe12a9 // indirect github.com/mattn/go-colorable v0.1.6 // indirect github.com/mitchellh/go-homedir v1.1.0 - github.com/mr-tron/base58 v1.1.3 + github.com/mr-tron/base58 v1.2.0 github.com/nbutton23/zxcvbn-go v0.0.0-20180912185939-ae427f1e4c1d github.com/olekukonko/tablewriter v0.0.4 // indirect github.com/pborman/uuid v1.2.0 // indirect diff --git a/go.sum b/go.sum index 774be35..d165208 100644 --- a/go.sum +++ b/go.sum @@ -2,13 +2,16 @@ cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMT github.com/AppsFlyer/go-sundheit v0.2.0 h1:FArqX+HbqZ6U32RC3giEAWRUpkggqxHj91KIvxNgwjU= github.com/AppsFlyer/go-sundheit v0.2.0/go.mod h1:rCRkVTMQo7/krF7xQ9X0XEF1an68viFR6/Gy02q+4ds= github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= +github.com/Microsoft/go-winio v0.4.11 h1:zoIOcVf0xPN1tnMVbTtEdI+P8OofVk3NObnwOQ6nK2Q= github.com/Microsoft/go-winio v0.4.11/go.mod h1:VhR8bwka0BXejwEJY73c50VrPtXAaKcyvVC4A4RozmA= github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU= github.com/Shopify/sarama v1.26.1/go.mod h1:NbSGBSSndYaIhRcBtY9V0U7AyH+x71bG668AuWys/yU= github.com/Shopify/toxiproxy v2.1.4+incompatible/go.mod h1:OXgGpZ6Cli1/URJOF1DMxUHB2q5Ap20/P/eIdh4G0pI= github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc= +github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751 h1:JYp7IbQjafoB+tBA3gMyHYHrpOtNuDiK/uB5uXxq5wM= github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc= github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0= +github.com/alecthomas/units v0.0.0-20190717042225-c3de453c63f4 h1:Hs82Z41s6SdL1CELW+XaDYmOH4hkBN4/N9og/AsOv7E= github.com/alecthomas/units v0.0.0-20190717042225-c3de453c63f4/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0= github.com/allegro/bigcache v1.2.1 h1:hg1sY1raCwic3Vnsvje6TT7/pnZba83LeFck5NrFKSc= github.com/allegro/bigcache v1.2.1/go.mod h1:Cb/ax3seSYIx7SuZdm2G2xzfwmv3TPSk2ucNfQESPXM= @@ -19,6 +22,8 @@ github.com/aristanetworks/goarista v0.0.0-20200520141224-0f14e646773f/go.mod h1: github.com/aristanetworks/splunk-hec-go v0.3.3/go.mod h1:1VHO9r17b0K7WmOlLb9nTk/2YanvOEnLMUgsFrxBROc= github.com/ava-labs/coreth v0.2.4 h1:MhnbuRyMcij7WU4+frayp40quc44AMPc4IrxXhmucWw= github.com/ava-labs/coreth v0.2.4/go.mod h1:pGolKipwq5vGIY2IBBcBkMYrqniXMsS5SBn+BBi4+Js= +github.com/ava-labs/coreth v0.2.5 h1:2Al753rpPHvvZfcz7w96YbKhGFvrcZzsIZ/sIp0A0Ao= +github.com/ava-labs/coreth v0.2.5/go.mod h1:pGolKipwq5vGIY2IBBcBkMYrqniXMsS5SBn+BBi4+Js= github.com/ava-labs/go-ethereum v1.9.3 h1:GmnMZ/dlvVAPFmWBzEpRJX49pUAymPfoASLNRJqR0AY= github.com/ava-labs/go-ethereum v1.9.3/go.mod h1:a+agc6fXfZFsPZCylA3ry4Y8CLCqLKg3Rc23NXZ9aw8= github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q= @@ -38,7 +43,9 @@ github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs github.com/deckarep/golang-set v1.7.1 h1:SCQV0S6gTtp6itiFrTqI+pfmJ4LN85S1YzhDf9rTHJQ= github.com/deckarep/golang-set v1.7.1/go.mod h1:93vsz/8Wt4joVM7c2AVqh+YRMiUSc14yDtF28KmMOgQ= github.com/decred/dcrd v1.3.0 h1:EEXm7BdiROfazDtuFsOu9mfotnyy00bgCuVwUqaszFo= +github.com/decred/dcrd/chaincfg/chainhash v1.0.2 h1:rt5Vlq/jM3ZawwiacWjPa+smINyLRN07EO0cNBV6DGU= github.com/decred/dcrd/chaincfg/chainhash v1.0.2/go.mod h1:BpbrGgrPTr3YJYRN3Bm+D9NuaFd+zGyNeIKgrhCXK60= +github.com/decred/dcrd/crypto/blake256 v1.0.0 h1:/8DMNYp9SGi5f0w7uCm6d6M4OU2rGFK09Y2A4Xv7EE0= github.com/decred/dcrd/crypto/blake256 v1.0.0/go.mod h1:sQl2p6Y26YV+ZOcSTP6thNdn47hh8kt6rqSlvmrXFAc= github.com/decred/dcrd/dcrec v1.0.0 h1:W+z6Es+Rai3MXYVoPAxYr5U1DGis0Co33scJ6uH2J6o= github.com/decred/dcrd/dcrec/secp256k1 v1.0.3 h1:u4XpHqlscRolxPxt2YHrFBDVZYY1AK+KMV02H1r+HmU= @@ -61,8 +68,10 @@ github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1m github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c= github.com/fjl/memsize v0.0.0-20190710130421-bcb5799ab5e5 h1:FtmdgXiUlNeRsoNMFlKLDt+S+6hbjVMEW6RGQ7aUf7c= github.com/fjl/memsize v0.0.0-20190710130421-bcb5799ab5e5/go.mod h1:VvhXpOYNQvB+uIk2RvXzuaQtkQJzzIx6lSBe1xv7hi0= +github.com/fortytw2/leaktest v1.3.0 h1:u8491cBMTQ8ft8aeV+adlcytMZylmA5nnwwkRZjI8vw= github.com/fortytw2/leaktest v1.3.0/go.mod h1:jDsjWgpAGjm2CA7WthBh/CdZYEPF31XHquHwclZch5g= github.com/frankban/quicktest v1.7.2/go.mod h1:jaStnuzAqU1AJdCO0l53JDCJrVDKcS03DbaAcR7Ks/o= +github.com/fsnotify/fsnotify v1.4.7 h1:IXs+QLmnXW2CcXuY+8Mzv/fWEsPGWxqefPtCP5CnV9I= github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo= github.com/garyburd/redigo v1.6.0/go.mod h1:NR3MbYisc3/PwhQ00EMzDiPmrwpPxAn5GI05/YaO1SY= github.com/gballet/go-libpcsclite v0.0.0-20191108122812-4678299bea08 h1:f6D9Hr8xV8uYKlyuj8XIruxlh9WjVjdh1gIicAS7ays= @@ -76,6 +85,7 @@ github.com/go-stack/stack v1.8.0 h1:5SgMzNM5HxrEjV0ww2lTmX6E2Izsfxas4+YHWRs3Lsk= github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY= github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ= github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q= +github.com/golang/groupcache v0.0.0-20190702054246-869f871628b6 h1:ZgQEtGgCBiWRM39fZuwSd1LwSqqSW0hOdXCYYDX0R3I= github.com/golang/groupcache v0.0.0-20190702054246-869f871628b6/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc= github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A= github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= @@ -97,6 +107,7 @@ github.com/golang/snappy v0.0.1/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEW github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M= github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= +github.com/google/go-cmp v0.4.0 h1:xsAVV57WRhGj6kEIi8ReJzQlHHqcBYCElAvkovg3B/4= github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= github.com/google/uuid v1.0.0 h1:b4Gk+7WdP/d3HZH8EJsZpvV7EtDOgaZLtnaNGIu1adA= @@ -119,6 +130,7 @@ github.com/hashicorp/golang-lru v0.5.4 h1:YDjusn29QI/Das2iO9M0BHnIbxPeyuCHsjMW+l github.com/hashicorp/golang-lru v0.5.4/go.mod h1:iADmTwqILo4mZ8BN3D2Q6+9jd8WM5uGBxy+E8yxSoD4= github.com/hashicorp/yamux v0.0.0-20180604194846-3520598351bb h1:b5rjCoWHc7eqmAS4/qyk21ZsHyb6Mxv/jykxvNTkU4M= github.com/hashicorp/yamux v0.0.0-20180604194846-3520598351bb/go.mod h1:+NfK9FKeTrX5uv1uIXGdwYDTeHna2qgaIlx54MXqjAM= +github.com/hpcloud/tail v1.0.0 h1:nfCOvKYfkgYP8hkirhJocXT2+zOD8yUNjXaWfTlyFKI= github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU= github.com/huin/goupnp v1.0.0 h1:wg75sLpL6DZqwHQN6E1Cfk6mtfzS45z8OV+ic+DtHRo= github.com/huin/goupnp v1.0.0/go.mod h1:n9v9KO1tAxYH82qOn+UTIFQDmx5n1Zxd/ClZDMX7Bnc= @@ -129,6 +141,7 @@ github.com/jackpal/gateway v1.0.6/go.mod h1:lTpwd4ACLXmpyiCTRtfiNyVnUmqT9RivzCDQ github.com/jackpal/go-nat-pmp v1.0.2 h1:KzKSgb7qkJvOUTqYl9/Hg/me3pWgBmERKrTGD7BdWus= github.com/jackpal/go-nat-pmp v1.0.2/go.mod h1:QPH045xvCAeXUZOxsnwmrtiCoxIr9eob+4orBN1SBKc= github.com/jcmturner/gofork v1.0.0/go.mod h1:MK8+TM0La+2rjBD4jE12Kj1pCCxK7d2LK/UM3ncEo0o= +github.com/jhump/protoreflect v1.6.0 h1:h5jfMVslIg6l29nsMs0D8Wj17RDVdNYti0vDN/PZZoE= github.com/jhump/protoreflect v1.6.0/go.mod h1:eaTn3RZAmMBcV0fifFvlm6VHNz3wSkYyXYWUh7ymB74= github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU= github.com/json-iterator/go v1.1.9/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4= @@ -142,8 +155,10 @@ github.com/klauspost/reedsolomon v1.9.3/go.mod h1:CwCi+NUr9pqSVktrkN+Ondf06rkhYZ github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515/go.mod h1:+0opPa2QZZtGFBFZlji/RkVcI2GknAs/DXo4wKdlNEc= github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= +github.com/kr/pretty v0.2.0 h1:s5hAObm+yFO5uHYt5dYjxi2rXrsnmRpJx4OYvIWUaQs= github.com/kr/pretty v0.2.0/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI= github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= +github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw= github.com/mattn/go-colorable v0.1.6 h1:6Su7aK7lXmJ/U79bYtBjLNaha4Fs1Rg9plHpcH+vvnE= @@ -164,6 +179,8 @@ github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lN github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0= github.com/mr-tron/base58 v1.1.3 h1:v+sk57XuaCKGXpWtVBX8YJzO7hMGx4Aajh4TQbdEFdc= github.com/mr-tron/base58 v1.1.3/go.mod h1:BinMc/sQntlIE1frQmRFPUoPA1Zkr8VRgBdjWI2mNwc= +github.com/mr-tron/base58 v1.2.0 h1:T/HDJBh4ZCPbU39/+c3rRvE0uKBQlU27+QI8LJ4t64o= +github.com/mr-tron/base58 v1.2.0/go.mod h1:BinMc/sQntlIE1frQmRFPUoPA1Zkr8VRgBdjWI2mNwc= github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U= github.com/nbutton23/zxcvbn-go v0.0.0-20180912185939-ae427f1e4c1d h1:AREM5mwr4u1ORQBMvzfzBgpsctsbQikCVpvC+tX285E= github.com/nbutton23/zxcvbn-go v0.0.0-20180912185939-ae427f1e4c1d/go.mod h1:o96djdrsSGy3AWPyBgZMAGfxZNfgntdJG+11KU4QvbU= @@ -174,8 +191,10 @@ github.com/olekukonko/tablewriter v0.0.4 h1:vHD/YYe1Wolo78koG299f7V/VAS08c6IpCLn github.com/olekukonko/tablewriter v0.0.4/go.mod h1:zq6QwlOf5SlnkVbMSr5EoBv3636FWnp+qbPhuoO21uA= github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= github.com/onsi/ginkgo v1.7.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= +github.com/onsi/ginkgo v1.10.1 h1:q/mM8GF/n0shIN8SaAZ0V+jnLPzen6WIVZdiwrRlMlo= github.com/onsi/ginkgo v1.10.1/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= github.com/onsi/gomega v1.4.3/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY= +github.com/onsi/gomega v1.7.0 h1:XPnZz8VVBHjVsy1vzJmRwIcSwiUO+JFfrv/xGiigmME= github.com/onsi/gomega v1.7.0/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY= github.com/openconfig/gnmi v0.0.0-20190823184014-89b2bf29312c/go.mod h1:t+O9It+LKzfOAhKTT5O0ehDix+MTqbtT0T9t+7zzOvc= github.com/openconfig/reference v0.0.0-20190727015836-8dfd928c9696/go.mod h1:ym2A+zigScwkSEb/cVQB0/ZMpU3rqiH6X7WRRsxgOGw= @@ -217,6 +236,7 @@ github.com/rs/cors v1.7.0 h1:+88SsELBHx5r+hZ8TCkggzSstaWNbDvThkVK8H6f9ik= github.com/rs/cors v1.7.0/go.mod h1:gFx+x8UowdsKA9AchylcLynDq+nNFfI8FkUZdN/jGCU= github.com/satori/go.uuid v1.2.0/go.mod h1:dA0hQrYB0VpLJoorglMZABFdXlWrHn1NEOzdhQKdks0= github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo= +github.com/sirupsen/logrus v1.4.2 h1:SPIRibHv4MatM3XXNO2BJeFLZwZ2LvZgfQ5+UNI2im4= github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE= github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA= github.com/status-im/keycard-go v0.0.0-20200402102358-957c09536969 h1:Oo2KZNP70KE0+IUJSidPj/BFS/RXNHmKIJOdckzml2E= @@ -226,6 +246,7 @@ github.com/steakknife/bloomfilter v0.0.0-20180922174646-6819c0d2a570/go.mod h1:8 github.com/steakknife/hamming v0.0.0-20180906055917-c99c65617cd3 h1:njlZPzLwU639dk2kqnCPPv+wNjq7Xb6EfUxe/oX0/NM= github.com/steakknife/hamming v0.0.0-20180906055917-c99c65617cd3/go.mod h1:hpGUWaI9xL8pRQCTXQgocU38Qw1g0Us7n5PxxTwTCYU= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/objx v0.1.1 h1:2vfRuCMp5sSVIDSqO8oNnWJq7mPa6KVP3iPIwFBuy8A= github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= @@ -310,6 +331,7 @@ golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3 golang.org/x/tools v0.0.0-20190524140312-2c0ae7006135/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q= golang.org/x/tools v0.0.0-20200221224223-e1da425f72fd/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28= golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM= google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4= @@ -336,10 +358,13 @@ google.golang.org/protobuf v1.20.1-0.20200309200217-e05f789c0967/go.mod h1:A+miE google.golang.org/protobuf v1.21.0/go.mod h1:47Nbq4nVaFHyn7ilMalzfO3qCViNmqZ2kzikPIcrTAo= google.golang.org/protobuf v1.23.0 h1:4MY060fB1DLGMB/7MBTLnwQUY6+F09GEiz6SsrNqyzM= google.golang.org/protobuf v1.23.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU= +gopkg.in/alecthomas/kingpin.v2 v2.2.6 h1:jMFz6MfLP0/4fUyZle81rXUoxOBFi19VUFKVDOQfozc= gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw= gopkg.in/bsm/ratelimit.v1 v1.0.0-20160220154919-db14e161995a/go.mod h1:KF9sEfUPAXdG8Oev9e99iLGnl2uJMjc5B+4y3O7x610= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo= gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/fsnotify.v1 v1.4.7 h1:xOHLXZwVvI9hhs+cLKq5+I5onOuwQLhQwiu63xxlHs4= gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys= gopkg.in/jcmturner/aescts.v1 v1.0.1/go.mod h1:nsR8qBOg+OucoIW+WMhB3GspUQXq9XorLnQb9XtvcOo= gopkg.in/jcmturner/dnsutils.v1 v1.0.1/go.mod h1:m3v+5svpVOhtFAP/wSz+yzh4Mc0Fg7eRhxkJMWSIz9Q= @@ -349,6 +374,7 @@ gopkg.in/jcmturner/rpc.v1 v1.1.0/go.mod h1:YIdkC4XfD6GXbzje11McwsDuOlZQSb9W4vfLv gopkg.in/olebedev/go-duktape.v3 v3.0.0-20200316214253-d7b0ff38cac9 h1:ITeyKbRetrVzqR3U1eY+ywgp7IBspGd1U/bkwd1gWu4= gopkg.in/olebedev/go-duktape.v3 v3.0.0-20200316214253-d7b0ff38cac9/go.mod h1:uAJfkITjFhyEEuUfm7bsmCZRbW5WRq8s9EY8HZ6hCns= gopkg.in/redis.v4 v4.2.4/go.mod h1:8KREHdypkCEojGKQcjMqAODMICIVwZAONWq8RowTITA= +gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkepLTh2hOroT7a+7czfdQ= gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw= gopkg.in/urfave/cli.v1 v1.20.0 h1:NdAVW6RYxDif9DhDHaAortIu956m2c0v+09AZBPTbE0= gopkg.in/urfave/cli.v1 v1.20.0/go.mod h1:vuBzUtMdQeixQj8LVd+/98pzhxNGQoyuPBlsXHOQNO0= diff --git a/ids/bag.go b/ids/bag.go index de0af46..1d16c64 100644 --- a/ids/bag.go +++ b/ids/bag.go @@ -8,6 +8,10 @@ import ( "strings" ) +const ( + minBagSize = 16 +) + // Bag is a multiset of IDs. // // A bag has the ability to split and filter on it's bits for ease of use for @@ -25,7 +29,7 @@ type Bag struct { func (b *Bag) init() { if b.counts == nil { - b.counts = make(map[[32]byte]int) + b.counts = make(map[[32]byte]int, minBagSize) } } @@ -72,16 +76,21 @@ func (b *Bag) AddCount(id ID, count int) { } // Count returns the number of times the id has been added. -func (b *Bag) Count(id ID) int { return b.counts[*id.ID] } +func (b *Bag) Count(id ID) int { + b.init() + return b.counts[*id.ID] +} // Len returns the number of times an id has been added. func (b *Bag) Len() int { return b.size } // List returns a list of all ids that have been added. func (b *Bag) List() []ID { - idList := []ID(nil) + idList := make([]ID, len(b.counts), len(b.counts)) + i := 0 for id := range b.counts { - idList = append(idList, NewID(id)) + idList[i] = NewID(id) + i++ } return idList } diff --git a/ids/bag_benchmark_test.go b/ids/bag_benchmark_test.go new file mode 100644 index 0000000..7007ddb --- /dev/null +++ b/ids/bag_benchmark_test.go @@ -0,0 +1,53 @@ +package ids + +import ( + "crypto/rand" + "testing" +) + +// +func BenchmarkBagListSmall(b *testing.B) { + smallLen := 5 + bag := Bag{} + for i := 0; i < smallLen; i++ { + var idBytes [32]byte + rand.Read(idBytes[:]) + NewID(idBytes) + bag.Add(NewID(idBytes)) + } + b.ResetTimer() + for n := 0; n < b.N; n++ { + bag.List() + } +} + +func BenchmarkBagListMedium(b *testing.B) { + mediumLen := 25 + bag := Bag{} + for i := 0; i < mediumLen; i++ { + var idBytes [32]byte + rand.Read(idBytes[:]) + NewID(idBytes) + bag.Add(NewID(idBytes)) + } + b.ResetTimer() + + for n := 0; n < b.N; n++ { + bag.List() + } +} + +func BenchmarkBagListLarge(b *testing.B) { + largeLen := 100000 + bag := Bag{} + for i := 0; i < largeLen; i++ { + var idBytes [32]byte + rand.Read(idBytes[:]) + NewID(idBytes) + bag.Add(NewID(idBytes)) + } + b.ResetTimer() + for n := 0; n < b.N; n++ { + bag.List() + } +} diff --git a/ids/bag_test.go b/ids/bag_test.go index ed35233..af0965b 100644 --- a/ids/bag_test.go +++ b/ids/bag_test.go @@ -18,8 +18,8 @@ func TestBagAdd(t *testing.T) { } else if count := bag.Count(id1); count != 0 { t.Fatalf("Bag.Count returned %d expected %d", count, 0) } else if size := bag.Len(); size != 0 { - t.Fatalf("Bag.Len returned %d expected %d", count, 0) - } else if list := bag.List(); list != nil { + t.Fatalf("Bag.Len returned %d elements expected %d", count, 0) + } else if list := bag.List(); len(list) != 0 { t.Fatalf("Bag.List returned %v expected %v", list, nil) } else if mode, freq := bag.Mode(); !mode.IsZero() { t.Fatalf("Bag.Mode[0] returned %s expected %s", mode, ID{}) diff --git a/ids/set.go b/ids/set.go index 9d0b1ec..d632949 100644 --- a/ids/set.go +++ b/ids/set.go @@ -7,11 +7,19 @@ import ( "strings" ) +const ( + // The minimum capacity of a set + minSetSize = 16 +) + // Set is a set of IDs type Set map[[32]byte]bool func (ids *Set) init(size int) { if *ids == nil { + if minSetSize > size { + size = minSetSize + } *ids = make(map[[32]byte]bool, size) } } @@ -70,9 +78,32 @@ func (ids *Set) Clear() { *ids = nil } // List converts this set into a list func (ids Set) List() []ID { - idList := []ID(nil) + idList := make([]ID, ids.Len()) + i := 0 for id := range ids { - idList = append(idList, NewID(id)) + idList[i] = NewID(id) + i++ + } + return idList +} + +// CappedList returns a list of length at most [size]. +// Size should be >= 0. If size < 0, returns nil. +func (ids Set) CappedList(size int) []ID { + if size < 0 { + return nil + } + if l := ids.Len(); l < size { + size = l + } + i := 0 + idList := make([]ID, size) + for id := range ids { + if i >= size { + break + } + idList[i] = NewID(id) + i++ } return idList } diff --git a/ids/set_benchmark_test.go b/ids/set_benchmark_test.go new file mode 100644 index 0000000..17c1c7a --- /dev/null +++ b/ids/set_benchmark_test.go @@ -0,0 +1,53 @@ +package ids + +import ( + "crypto/rand" + "testing" +) + +// +func BenchmarkSetListSmall(b *testing.B) { + smallLen := 5 + set := Set{} + for i := 0; i < smallLen; i++ { + var idBytes [32]byte + rand.Read(idBytes[:]) + NewID(idBytes) + set.Add(NewID(idBytes)) + } + b.ResetTimer() + for n := 0; n < b.N; n++ { + set.List() + } +} + +func BenchmarkSetListMedium(b *testing.B) { + mediumLen := 25 + set := Set{} + for i := 0; i < mediumLen; i++ { + var idBytes [32]byte + rand.Read(idBytes[:]) + NewID(idBytes) + set.Add(NewID(idBytes)) + } + b.ResetTimer() + + for n := 0; n < b.N; n++ { + set.List() + } +} + +func BenchmarkSetListLarge(b *testing.B) { + largeLen := 100000 + set := Set{} + for i := 0; i < largeLen; i++ { + var idBytes [32]byte + rand.Read(idBytes[:]) + NewID(idBytes) + set.Add(NewID(idBytes)) + } + b.ResetTimer() + for n := 0; n < b.N; n++ { + set.List() + } +} diff --git a/ids/set_test.go b/ids/set_test.go index 3c7ab15..b4e05db 100644 --- a/ids/set_test.go +++ b/ids/set_test.go @@ -55,3 +55,46 @@ func TestSet(t *testing.T) { t.Fatalf("Sets overlap") } } + +func TestSetCappedList(t *testing.T) { + set := Set{} + + id := Empty + + if list := set.CappedList(0); len(list) != 0 { + t.Fatalf("List should have been empty but was %v", list) + } + + set.Add(id) + + if list := set.CappedList(0); len(list) != 0 { + t.Fatalf("List should have been empty but was %v", list) + } else if list := set.CappedList(1); len(list) != 1 { + t.Fatalf("List should have had length %d but had %d", 1, len(list)) + } else if returnedID := list[0]; !id.Equals(returnedID) { + t.Fatalf("List should have been %s but was %s", id, returnedID) + } else if list := set.CappedList(2); len(list) != 1 { + t.Fatalf("List should have had length %d but had %d", 1, len(list)) + } else if returnedID := list[0]; !id.Equals(returnedID) { + t.Fatalf("List should have been %s but was %s", id, returnedID) + } + + id2 := NewID([32]byte{1}) + set.Add(id2) + + if list := set.CappedList(0); len(list) != 0 { + t.Fatalf("List should have been empty but was %v", list) + } else if list := set.CappedList(1); len(list) != 1 { + t.Fatalf("List should have had length %d but had %d", 1, len(list)) + } else if returnedID := list[0]; !id.Equals(returnedID) && !id2.Equals(returnedID) { + t.Fatalf("List should have been %s but was %s", id, returnedID) + } else if list := set.CappedList(2); len(list) != 2 { + t.Fatalf("List should have had length %d but had %d", 2, len(list)) + } else if list := set.CappedList(3); len(list) != 2 { + t.Fatalf("List should have had length %d but had %d", 2, len(list)) + } else if returnedID := list[0]; !id.Equals(returnedID) && !id2.Equals(returnedID) { + t.Fatalf("list contains unexpected element %s", returnedID) + } else if returnedID := list[1]; !id.Equals(returnedID) && !id2.Equals(returnedID) { + t.Fatalf("list contains unexpected element %s", returnedID) + } +} diff --git a/ids/short_set.go b/ids/short_set.go index 690cc3a..9bcd37d 100644 --- a/ids/short_set.go +++ b/ids/short_set.go @@ -5,11 +5,18 @@ package ids import "strings" +const ( + minShortSetSize = 16 +) + // ShortSet is a set of ShortIDs type ShortSet map[[20]byte]bool func (ids *ShortSet) init(size int) { if *ids == nil { + if minShortSetSize > size { + size = minShortSetSize + } *ids = make(map[[20]byte]bool, size) } } @@ -50,24 +57,34 @@ func (ids *ShortSet) Remove(idList ...ShortID) { // Clear empties this set func (ids *ShortSet) Clear() { *ids = nil } -// CappedList returns a list of length at most [size]. Size should be >= 0 +// CappedList returns a list of length at most [size]. +// Size should be >= 0. If size < 0, returns nil. func (ids ShortSet) CappedList(size int) []ShortID { - idList := make([]ShortID, size)[:0] + if size < 0 { + return nil + } + if l := ids.Len(); l < size { + size = l + } + i := 0 + idList := make([]ShortID, size) for id := range ids { - if size <= 0 { + if i >= size { break } - size-- - idList = append(idList, NewShortID(id)) + idList[i] = NewShortID(id) + i++ } return idList } // List converts this set into a list func (ids ShortSet) List() []ShortID { - idList := make([]ShortID, len(ids))[:0] + idList := make([]ShortID, len(ids), len(ids)) + i := 0 for id := range ids { - idList = append(idList, NewShortID(id)) + idList[i] = NewShortID(id) + i++ } return idList } diff --git a/ids/unique_bag.go b/ids/unique_bag.go index d5d3e36..461a6bc 100644 --- a/ids/unique_bag.go +++ b/ids/unique_bag.go @@ -8,12 +8,16 @@ import ( "strings" ) +const ( + minUniqueBagSize = 16 +) + // UniqueBag ... type UniqueBag map[[32]byte]BitSet func (b *UniqueBag) init() { if *b == nil { - *b = make(map[[32]byte]BitSet) + *b = make(map[[32]byte]BitSet, minUniqueBagSize) } } diff --git a/main/main.go b/main/main.go index 5aca025..250c1e0 100644 --- a/main/main.go +++ b/main/main.go @@ -40,12 +40,11 @@ func main() { defer log.StopOnPanic() defer Config.DB.Close() - if Config.StakingIP.IsZero() { - log.Warn("NAT traversal has failed. It will be able to connect to less nodes.") - } - // Track if sybil control is enforced - if !Config.EnableStaking { + if !Config.EnableStaking && Config.EnableP2PTLS { + log.Warn("Staking is disabled. Sybil control is not enforced.") + } + if !Config.EnableStaking && !Config.EnableP2PTLS { log.Warn("Staking and p2p encryption are disabled. Packet spoofing is possible.") } @@ -65,11 +64,19 @@ func main() { log.Debug("assertions are enabled. This may slow down execution") } - mapper := nat.NewDefaultMapper(log, Config.Nat, nat.TCP, "gecko") + mapper := nat.NewPortMapper(log, Config.Nat) defer mapper.UnmapAllPorts() - mapper.MapPort(Config.StakingIP.Port, Config.StakingIP.Port) - mapper.MapPort(Config.HTTPPort, Config.HTTPPort) + port, err := mapper.Map("TCP", Config.StakingLocalPort, "gecko-staking") // Open staking port + if err == nil { + Config.StakingIP.Port = port + } else { + log.Warn("NAT traversal has failed. The node will be able to connect to less nodes.") + } + + if Config.HTTPHost != "127.0.0.1" && Config.HTTPHost != "localhost" { // Open HTTP port iff HTTP server not listening on localhost + _, _ = mapper.Map("TCP", Config.HTTPPort, "gecko-http") + } node := node.Node{} diff --git a/main/params.go b/main/params.go index eef8e60..47ea5f8 100644 --- a/main/params.go +++ b/main/params.go @@ -35,21 +35,25 @@ const ( // Results of parsing the CLI var ( - Config = node.Config{} - Err error - defaultDbDir = os.ExpandEnv(filepath.Join("$HOME", ".gecko", "db")) - defaultStakingKeyPath = os.ExpandEnv(filepath.Join("$HOME", ".gecko", "staking", "staker.key")) - defaultStakingCertPath = os.ExpandEnv(filepath.Join("$HOME", ".gecko", "staking", "staker.crt")) + Config = node.Config{} + Err error + defaultNetworkName = genesis.TestnetName - defaultPluginDirs = []string{ - "./build/plugins", - "./plugins", - os.ExpandEnv(filepath.Join("$HOME", ".gecko", "plugins")), + homeDir = os.ExpandEnv("$HOME") + defaultDbDir = filepath.Join(homeDir, ".gecko", "db") + defaultStakingKeyPath = filepath.Join(homeDir, ".gecko", "staking", "staker.key") + defaultStakingCertPath = filepath.Join(homeDir, ".gecko", "staking", "staker.crt") + defaultPluginDirs = []string{ + filepath.Join(".", "build", "plugins"), + filepath.Join(".", "plugins"), + filepath.Join("/", "usr", "local", "lib", "gecko"), + filepath.Join(homeDir, ".gecko", "plugins"), } ) var ( - errBootstrapMismatch = errors.New("more bootstrap IDs provided than bootstrap IPs") + errBootstrapMismatch = errors.New("more bootstrap IDs provided than bootstrap IPs") + errStakingRequiresTLS = errors.New("if staking is enabled, network TLS must also be enabled") ) // GetIPs returns the default IPs for each network @@ -169,7 +173,7 @@ func init() { version := fs.Bool("version", false, "If true, print version and quit") // NetworkID: - networkName := fs.String("network-id", genesis.TestnetName, "Network ID this node will connect to") + networkName := fs.String("network-id", defaultNetworkName, "Network ID this node will connect to") // Ava fees: fs.Uint64Var(&Config.AvaTxFee, "ava-tx-fee", 0, "Ava transaction fee, in $nAva") @@ -188,7 +192,7 @@ func init() { consensusIP := fs.String("public-ip", "", "Public IP of this node") // HTTP Server: - httpHost := fs.String("http-host", "", "Address of the HTTP server") + httpHost := fs.String("http-host", "127.0.0.1", "Address of the HTTP server") httpPort := fs.Uint("http-port", 9650, "Port of the HTTP server") fs.BoolVar(&Config.EnableHTTPS, "http-tls-enabled", false, "Upgrade the HTTP server to HTTPs") fs.StringVar(&Config.HTTPSKeyFile, "http-tls-key-file", "", "TLS private key file for the HTTPs server") @@ -200,7 +204,9 @@ func init() { // Staking: consensusPort := fs.Uint("staking-port", 9651, "Port of the consensus server") - fs.BoolVar(&Config.EnableStaking, "staking-tls-enabled", true, "Require TLS to authenticate staking connections") + // TODO - keeping same flag for backwards compatibility, should be changed to "staking-enabled" + fs.BoolVar(&Config.EnableStaking, "staking-tls-enabled", true, "Enable staking. If enabled, Network TLS is required.") + fs.BoolVar(&Config.EnableP2PTLS, "p2p-tls-enabled", true, "Require TLS to authenticate network communication") fs.StringVar(&Config.StakingKeyFile, "staking-tls-key-file", defaultStakingKeyPath, "TLS private key for staking") fs.StringVar(&Config.StakingCertFile, "staking-tls-cert-file", defaultStakingCertPath, "TLS certificate for staking") @@ -221,7 +227,8 @@ func init() { fs.IntVar(&Config.ConsensusParams.ConcurrentRepolls, "snow-concurrent-repolls", 1, "Minimum number of concurrent polls for finalizing consensus") // Enable/Disable APIs: - fs.BoolVar(&Config.AdminAPIEnabled, "api-admin-enabled", true, "If true, this node exposes the Admin API") + fs.BoolVar(&Config.AdminAPIEnabled, "api-admin-enabled", false, "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") @@ -234,7 +241,15 @@ func init() { ferr := fs.Parse(os.Args[1:]) if *version { // If --version used, print version and exit - fmt.Println(node.Version.String()) + networkID, err := genesis.NetworkID(defaultNetworkName) + if errs.Add(err); err != nil { + return + } + networkGeneration := genesis.NetworkName(networkID) + fmt.Printf( + "%s [database=%s, network=%s/%s]\n", + node.Version, dbVersion, defaultNetworkName, networkGeneration, + ) os.Exit(0) } @@ -269,16 +284,16 @@ func init() { Config.DB = memdb.New() } - Config.Nat = nat.NewRouter() - var ip net.IP // If public IP is not specified, get it using shell command dig if *consensusIP == "" { - ip, err = Config.Nat.IP() + Config.Nat = nat.GetRouter() + ip, err = Config.Nat.ExternalIP() if err != nil { ip = net.IPv4zero // Couldn't get my IP...set to 0.0.0.0 } } else { + Config.Nat = nat.NewNoRouter() ip = net.ParseIP(*consensusIP) } @@ -291,6 +306,7 @@ func init() { IP: ip, Port: uint16(*consensusPort), } + Config.StakingLocalPort = uint16(*consensusPort) defaultBootstrapIPs, defaultBootstrapIDs := GetDefaultBootstraps(networkID, 5) @@ -318,7 +334,13 @@ func init() { *bootstrapIDs = strings.Join(defaultBootstrapIDs, ",") } } - if Config.EnableStaking { + + if Config.EnableStaking && !Config.EnableP2PTLS { + errs.Add(errStakingRequiresTLS) + return + } + + if Config.EnableP2PTLS { i := 0 cb58 := formatting.CB58{} for _, id := range strings.Split(*bootstrapIDs, ",") { diff --git a/nat/mapper.go b/nat/mapper.go deleted file mode 100644 index 3beaedd..0000000 --- a/nat/mapper.go +++ /dev/null @@ -1,143 +0,0 @@ -// (c) 2019-2020, Ava Labs, Inc. All rights reserved. -// See the file LICENSE for licensing terms. - -package nat - -import ( - "sync" - "time" - - "github.com/ava-labs/gecko/utils/logging" - "github.com/ava-labs/gecko/utils/wrappers" -) - -const ( - defaultMappingTimeout = 30 * time.Minute - defaultMappingUpdateInterval = 3 * defaultMappingTimeout / 4 -) - -// Mapper maps port -type Mapper interface { - MapPort(newInternalPort, newExternalPort uint16) error - UnmapAllPorts() error -} - -type mapper struct { - log logging.Logger - router Router - networkProtocol NetworkProtocol - mappingNames string - mappingTimeout time.Duration - mappingUpdateInterval time.Duration - - closer chan struct{} - wg sync.WaitGroup - errLock sync.Mutex - errs wrappers.Errs -} - -// NewMapper returns a new mapper that can map ports on a router -func NewMapper( - log logging.Logger, - router Router, - networkProtocol NetworkProtocol, - mappingNames string, - mappingTimeout time.Duration, - mappingUpdateInterval time.Duration, -) Mapper { - return &mapper{ - log: log, - router: router, - networkProtocol: networkProtocol, - mappingNames: mappingNames, - mappingTimeout: mappingTimeout, - mappingUpdateInterval: mappingUpdateInterval, - closer: make(chan struct{}), - } -} - -// NewDefaultMapper returns a new mapper that can map ports on a router with -// default settings -func NewDefaultMapper( - log logging.Logger, - router Router, - networkProtocol NetworkProtocol, - mappingNames string, -) Mapper { - return NewMapper( - log, - router, - networkProtocol, - mappingNames, - defaultMappingTimeout, // uses the default value - defaultMappingUpdateInterval, // uses the default value - ) -} - -// MapPort maps a local port to a port on the router until UnmapAllPorts is -// called. -func (m *mapper) MapPort(newInternalPort, newExternalPort uint16) error { - m.wg.Add(1) - go m.mapPort(newInternalPort, newExternalPort) - return nil -} - -func (m *mapper) mapPort(newInternalPort, newExternalPort uint16) { - // duration is set to 0 here so that the select case will execute - // immediately - updateTimer := time.NewTimer(0) - defer func() { - updateTimer.Stop() - - m.errLock.Lock() - m.errs.Add(m.router.UnmapPort( - m.networkProtocol, - newInternalPort, - newExternalPort)) - m.errLock.Unlock() - - m.log.Debug("Unmapped external port %d to internal port %d", - newExternalPort, - newInternalPort) - - m.wg.Done() - }() - - for { - select { - case <-updateTimer.C: - err := m.router.MapPort( - m.networkProtocol, - newInternalPort, - newExternalPort, - m.mappingNames, - m.mappingTimeout) - - if err != nil { - m.errLock.Lock() - m.errs.Add(err) - m.errLock.Unlock() - - m.log.Debug("Failed to add mapping from external port %d to internal port %d due to %s", - newExternalPort, - newInternalPort, - err) - } else { - m.log.Debug("Mapped external port %d to internal port %d", - newExternalPort, - newInternalPort) - } - - // remap the port in m.mappingUpdateInterval - updateTimer.Reset(m.mappingUpdateInterval) - case _, _ = <-m.closer: - return // only return when all ports are unmapped - } - } -} - -func (m *mapper) UnmapAllPorts() error { - close(m.closer) - m.wg.Wait() - return m.errs.Err -} diff --git a/nat/nat.go b/nat/nat.go new file mode 100644 index 0000000..fd4516b --- /dev/null +++ b/nat/nat.go @@ -0,0 +1,139 @@ +// (c) 2019-2020, Ava Labs, Inc. All rights reserved. +// See the file LICENSE for licensing terms. + +package nat + +import ( + "errors" + "net" + "sync" + "time" + + "github.com/ava-labs/gecko/utils/logging" +) + +const ( + mapTimeout = 30 * time.Second + mapUpdateTimeout = mapTimeout / 2 + maxRetries = 20 +) + +// Router describes the functionality that a network device must support to be +// able to open ports to an external IP. +type Router interface { + MapPort(protocol string, intPort, extPort uint16, desc string, duration time.Duration) error + UnmapPort(protocol string, intPort, extPort uint16) error + ExternalIP() (net.IP, error) + GetPortMappingEntry(extPort uint16, protocol string) ( + InternalIP string, + InternalPort uint16, + Description string, + err error, + ) +} + +// GetRouter returns a router on the current network. +func GetRouter() Router { + if r := getUPnPRouter(); r != nil { + return r + } + if r := getPMPRouter(); r != nil { + return r + } + + return NewNoRouter() +} + +// Mapper attempts to open a set of ports on a router +type Mapper struct { + log logging.Logger + r Router + closer chan struct{} + wg sync.WaitGroup +} + +// NewPortMapper returns an initialized mapper +func NewPortMapper(log logging.Logger, r Router) Mapper { + return Mapper{ + log: log, + r: r, + closer: make(chan struct{}), + } +} + +// Map sets up port mapping using given protocol, internal and external ports +// and returns the final port mapped. It returns 0 if mapping failed after the +// maximun number of retries +func (dev *Mapper) Map(protocol string, intPort uint16, desc string) (uint16, error) { + mappedPort := make(chan uint16) + + go dev.keepPortMapping(mappedPort, protocol, intPort, desc) + + port := <-mappedPort + if port == 0 { + return 0, errors.New("failed to map port") + } + return port, nil +} + +// keepPortMapping runs in the background to keep a port mapped. It renews the +// the port mapping in mapUpdateTimeout. +func (dev *Mapper) keepPortMapping(mappedPort chan<- uint16, protocol string, + intPort uint16, desc string) { + updateTimer := time.NewTimer(mapUpdateTimeout) + + for i := 0; i <= maxRetries; i++ { + extPort := intPort + uint16(i) + if intaddr, intPort, desc, err := dev.r.GetPortMappingEntry(extPort, protocol); err == nil { + dev.log.Debug("Port %d is taken by %s:%d: %s, retry with the next port", + extPort, intaddr, intPort, desc) + } else if err := dev.r.MapPort(protocol, intPort, extPort, desc, mapTimeout); err != nil { + dev.log.Debug("Map port failed. Protocol %s Internal %d External %d. %s", + protocol, intPort, extPort, err) + } else { + dev.log.Info("Mapped Protocol %s Internal %d External %d.", protocol, + intPort, extPort) + + dev.wg.Add(1) + + mappedPort <- extPort + + defer func(extPort uint16) { + updateTimer.Stop() + + dev.log.Debug("Unmap protocol %s external port %d", protocol, extPort) + dev.r.UnmapPort(protocol, intPort, extPort) + + dev.wg.Done() + }(extPort) + + for { + select { + case <-updateTimer.C: + if err := dev.r.MapPort(protocol, intPort, extPort, desc, mapTimeout); err != nil { + dev.log.Error("Renewing port mapping from external port %d to internal port %d failed with %s", + intPort, extPort, err) + } else { + dev.log.Debug("Renewed port mapping from external port %d to internal port %d.", + intPort, extPort) + } + + updateTimer.Reset(mapUpdateTimeout) + case _, _ = <-dev.closer: + return + } + } + } + } + + dev.log.Debug("Unable to map port %d~%d", intPort, intPort+maxRetries) + mappedPort <- 0 +} + +// UnmapAllPorts stops mapping all ports from this mapper and attempts to unmap +// them. +func (dev *Mapper) UnmapAllPorts() { + close(dev.closer) + dev.wg.Wait() + dev.log.Info("Unmapped all ports") +} diff --git a/nat/no_router.go b/nat/no_router.go index edb86b6..0971b46 100644 --- a/nat/no_router.go +++ b/nat/no_router.go @@ -4,25 +4,57 @@ package nat import ( - "errors" + "fmt" "net" "time" ) -var ( - errNoRouter = errors.New("no nat enabled router was discovered") -) +const googleDNSServer = "8.8.8.8:80" -type noRouter struct{} - -func (noRouter) MapPort(_ NetworkProtocol, _, _ uint16, _ string, _ time.Duration) error { - return errNoRouter +type noRouter struct { + ip net.IP } -func (noRouter) UnmapPort(_ NetworkProtocol, _, _ uint16) error { - return errNoRouter +func (noRouter) MapPort(_ string, intPort, extPort uint16, _ string, _ time.Duration) error { + if intPort != extPort { + return fmt.Errorf("cannot map port %d to %d", intPort, extPort) + } + return nil } -func (noRouter) IP() (net.IP, error) { - return nil, errNoRouter +func (noRouter) UnmapPort(string, uint16, uint16) error { + return nil +} + +func (r noRouter) ExternalIP() (net.IP, error) { + return r.ip, nil +} + +func (noRouter) GetPortMappingEntry(uint16, string) (string, uint16, string, error) { + return "", 0, "", nil +} + +func getOutboundIP() (net.IP, error) { + conn, err := net.Dial("udp", googleDNSServer) + if err != nil { + return nil, err + } + + if udpAddr, ok := conn.LocalAddr().(*net.UDPAddr); ok { + return udpAddr.IP, conn.Close() + } + + conn.Close() + return nil, fmt.Errorf("getting outbound IP failed") +} + +// NewNoRouter returns a router that assumes the network is public +func NewNoRouter() Router { + ip, err := getOutboundIP() + if err != nil { + return nil + } + return &noRouter{ + ip: ip, + } } diff --git a/nat/pmp.go b/nat/pmp.go index 311375d..ce40362 100644 --- a/nat/pmp.go +++ b/nat/pmp.go @@ -4,11 +4,13 @@ package nat import ( + "fmt" "net" "time" "github.com/jackpal/gateway" - "github.com/jackpal/go-nat-pmp" + + natpmp "github.com/jackpal/go-nat-pmp" ) var ( @@ -17,12 +19,12 @@ var ( // natPMPClient adapts the NAT-PMP protocol implementation so it conforms to // the common interface. -type pmpClient struct { +type pmpRouter struct { client *natpmp.Client } -func (pmp *pmpClient) MapPort( - networkProtocol NetworkProtocol, +func (pmp *pmpRouter) MapPort( + networkProtocol string, newInternalPort uint16, newExternalPort uint16, mappingName string, @@ -37,8 +39,8 @@ func (pmp *pmpClient) MapPort( return err } -func (pmp *pmpClient) UnmapPort( - networkProtocol NetworkProtocol, +func (pmp *pmpRouter) UnmapPort( + networkProtocol string, internalPort uint16, _ uint16) error { protocol := string(networkProtocol) @@ -48,7 +50,7 @@ func (pmp *pmpClient) UnmapPort( return err } -func (pmp *pmpClient) IP() (net.IP, error) { +func (pmp *pmpRouter) ExternalIP() (net.IP, error) { response, err := pmp.client.GetExternalAddress() if err != nil { return nil, err @@ -56,14 +58,20 @@ func (pmp *pmpClient) IP() (net.IP, error) { return response.ExternalIPAddress[:], nil } -func getPMPRouter() Router { +// go-nat-pmp does not support port mapping entry query +func (pmp *pmpRouter) GetPortMappingEntry(externalPort uint16, protocol string) ( + string, uint16, string, error) { + return "", 0, "", fmt.Errorf("port mapping entry not found") +} + +func getPMPRouter() *pmpRouter { gatewayIP, err := gateway.DiscoverGateway() if err != nil { return nil } - pmp := &pmpClient{natpmp.NewClientWithTimeout(gatewayIP, pmpClientTimeout)} - if _, err := pmp.IP(); err != nil { + pmp := &pmpRouter{natpmp.NewClientWithTimeout(gatewayIP, pmpClientTimeout)} + if _, err := pmp.ExternalIP(); err != nil { return nil } diff --git a/nat/router.go b/nat/router.go deleted file mode 100644 index 11b58f9..0000000 --- a/nat/router.go +++ /dev/null @@ -1,65 +0,0 @@ -// (c) 2019-2020, Ava Labs, Inc. All rights reserved. -// See the file LICENSE for licensing terms. - -// Package nat performs network address translation and provides helpers for -// routing ports. -package nat - -import ( - "net" - "time" -) - -// NetworkProtocol is a protocol that will be used through a port -type NetworkProtocol string - -// Available protocol -const ( - TCP NetworkProtocol = "TCP" - UDP NetworkProtocol = "UDP" -) - -// Router provides a standard NAT router functions. Specifically, allowing the -// fetching of public IPs and port forwarding to this computer. -type Router interface { - // mapPort creates a mapping between a port on the local computer to an - // external port on the router. - // - // The mappingName is something displayed on the router, so it is included - // for completeness. - MapPort( - networkProtocol NetworkProtocol, - newInternalPort uint16, - newExternalPort uint16, - mappingName string, - mappingDuration time.Duration) error - - // UnmapPort clears a mapping that was previous made by a call to MapPort - UnmapPort( - networkProtocol NetworkProtocol, - internalPort uint16, - externalPort uint16) error - - // Returns the routers IP address on the network the router considers - // external - IP() (net.IP, error) -} - -// NewRouter returns a new router discovered on the local network -func NewRouter() Router { - routers := make(chan Router) - // Because getting a router can take a noticeable amount of time to error, - // we run these requests in parallel - go func() { - routers <- getUPnPRouter() - }() - go func() { - routers <- getPMPRouter() - }() - for i := 0; i < 2; i++ { - if router := <-routers; router != nil { - return router - } - } - return noRouter{} -} diff --git a/nat/upnp.go b/nat/upnp.go index e60cd6e..3191bc8 100644 --- a/nat/upnp.go +++ b/nat/upnp.go @@ -4,7 +4,6 @@ package nat import ( - "errors" "fmt" "net" "time" @@ -15,11 +14,7 @@ import ( ) const ( - soapTimeout = time.Second -) - -var ( - errNoGateway = errors.New("Failed to connect to any avaliable gateways") + soapRequestTimeout = 3 * time.Second ) // upnpClient is the interface used by goupnp for their client implementations @@ -47,69 +42,30 @@ type upnpClient interface { // returns if there is rsip available, nat enabled, or an unexpected error. GetNATRSIPStatus() (newRSIPAvailable bool, natEnabled bool, err error) -} -type upnpRouter struct { - root *goupnp.RootDevice - client upnpClient -} - -func (n *upnpRouter) MapPort( - networkProtocol NetworkProtocol, - newInternalPort uint16, - newExternalPort uint16, - mappingName string, - mappingDuration time.Duration, -) error { - ip, err := n.localAddress() - if err != nil { - return err - } - - protocol := string(networkProtocol) - // goupnp uses seconds to denote their lifetime - lifetime := uint32(mappingDuration / time.Second) - - // UnmapPort's error is intentionally dropped, because the mapping may not - // exist. - n.UnmapPort(networkProtocol, newInternalPort, newExternalPort) - - return n.client.AddPortMapping( - "", // newRemoteHost isn't used to limit the mapping to a host - newExternalPort, - protocol, - newInternalPort, - ip.String(), // newInternalClient is the client traffic should be sent to - true, // newEnabled enables port mappings - mappingName, - lifetime, + // attempts to get port mapping information give a external port and protocol + GetSpecificPortMappingEntry( + NewRemoteHost string, + NewExternalPort uint16, + NewProtocol string, + ) ( + NewInternalPort uint16, + NewInternalClient string, + NewEnabled bool, + NewPortMappingDescription string, + NewLeaseDuration uint32, + err error, ) } -func (n *upnpRouter) UnmapPort(networkProtocol NetworkProtocol, _, externalPort uint16) error { - protocol := string(networkProtocol) - return n.client.DeletePortMapping( - "", // newRemoteHost isn't used to limit the mapping to a host - externalPort, - protocol) +type upnpRouter struct { + dev *goupnp.RootDevice + client upnpClient } -func (n *upnpRouter) IP() (net.IP, error) { - ipStr, err := n.client.GetExternalIPAddress() - if err != nil { - return nil, err - } - - ip := net.ParseIP(ipStr) - if ip == nil { - return nil, fmt.Errorf("invalid IP %s", ipStr) - } - return ip, nil -} - -func (n *upnpRouter) localAddress() (net.IP, error) { +func (r *upnpRouter) localIP() (net.IP, error) { // attempt to get an address on the router - deviceAddr, err := net.ResolveUDPAddr("udp4", n.root.URLBase.Host) + deviceAddr, err := net.ResolveUDPAddr("udp", r.dev.URLBase.Host) if err != nil { return nil, err } @@ -120,7 +76,7 @@ func (n *upnpRouter) localAddress() (net.IP, error) { return nil, err } - // attempt to find one of my ips that the router would know about + // attempt to find one of my IPs that matches router's record for _, netInterface := range netInterfaces { addrs, err := netInterface.Addrs() if err != nil { @@ -128,9 +84,6 @@ func (n *upnpRouter) localAddress() (net.IP, error) { } for _, addr := range addrs { - // this is pretty janky, but it seems to be the best way to get the - // ip mask and properly check if the ip references the device we are - // connected to ipNet, ok := addr.(*net.IPNet) if !ok { continue @@ -144,110 +97,119 @@ func (n *upnpRouter) localAddress() (net.IP, error) { return nil, fmt.Errorf("couldn't find the local address in the same network as %s", deviceIP) } -// getUPnPRouter searches for all Gateway Devices that have avaliable -// connections in the goupnp library and returns the first connection it can -// find. -func getUPnPRouter() Router { - routers := make(chan *upnpRouter) - // Because DiscoverDevices takes a noticeable amount of time to error, we - // run these requests in parallel - go func() { - routers <- connectToGateway(internetgateway1.URN_WANConnectionDevice_1, gateway1) - }() - go func() { - routers <- connectToGateway(internetgateway2.URN_WANConnectionDevice_2, gateway2) - }() - for i := 0; i < 2; i++ { - if router := <-routers; router != nil { - return router - } +func (r *upnpRouter) ExternalIP() (net.IP, error) { + str, err := r.client.GetExternalIPAddress() + if err != nil { + return nil, err } - return nil + + ip := net.ParseIP(str) + if ip == nil { + return nil, fmt.Errorf("invalid IP %s", str) + } + return ip, nil } -func gateway1(client goupnp.ServiceClient) upnpClient { +func (r *upnpRouter) MapPort(protocol string, intPort, extPort uint16, + desc string, duration time.Duration) error { + ip, err := r.localIP() + if err != nil { + return nil + } + lifetime := uint32(duration / time.Second) + + return r.client.AddPortMapping("", extPort, protocol, intPort, + ip.String(), true, desc, lifetime) +} + +func (r *upnpRouter) UnmapPort(protocol string, _, extPort uint16) error { + return r.client.DeletePortMapping("", extPort, protocol) +} + +func (r *upnpRouter) GetPortMappingEntry(extPort uint16, protocol string) (string, uint16, string, error) { + intPort, intAddr, _, desc, _, err := r.client.GetSpecificPortMappingEntry("", extPort, protocol) + return intAddr, intPort, desc, err +} + +// create UPnP SOAP service client with URN +func getUPnPClient(client goupnp.ServiceClient) upnpClient { switch client.Service.ServiceType { case internetgateway1.URN_WANIPConnection_1: return &internetgateway1.WANIPConnection1{ServiceClient: client} case internetgateway1.URN_WANPPPConnection_1: return &internetgateway1.WANPPPConnection1{ServiceClient: client} - default: - return nil - } -} - -func gateway2(client goupnp.ServiceClient) upnpClient { - switch client.Service.ServiceType { - case internetgateway2.URN_WANIPConnection_1: - return &internetgateway2.WANIPConnection1{ServiceClient: client} case internetgateway2.URN_WANIPConnection_2: return &internetgateway2.WANIPConnection2{ServiceClient: client} - case internetgateway2.URN_WANPPPConnection_1: - return &internetgateway2.WANPPPConnection1{ServiceClient: client} default: return nil } } -func connectToGateway(deviceType string, toClient func(goupnp.ServiceClient) upnpClient) *upnpRouter { - devs, err := goupnp.DiscoverDevices(deviceType) +// discover() tries to find gateway device +func discover(target string) *upnpRouter { + devs, err := goupnp.DiscoverDevices(target) if err != nil { return nil } - // we are iterating over all the network devices, acting a possible roots - for i := range devs { - dev := &devs[i] - if dev.Root == nil { + + router := make(chan *upnpRouter) + for i := 0; i < len(devs); i++ { + if devs[i].Root == nil { continue } + go func(dev *goupnp.MaybeRootDevice) { + var r *upnpRouter = nil + dev.Root.Device.VisitServices(func(service *goupnp.Service) { + c := goupnp.ServiceClient{ + SOAPClient: service.NewSOAPClient(), + RootDevice: dev.Root, + Location: dev.Location, + Service: service, + } + c.SOAPClient.HTTPClient.Timeout = soapRequestTimeout + client := getUPnPClient(c) + if client == nil { + return + } + if _, nat, err := client.GetNATRSIPStatus(); err != nil || !nat { + return + } + r = &upnpRouter{dev.Root, client} + }) + router <- r + }(&devs[i]) + } - // the root device may be a router, so attempt to connect to that - rootDevice := &dev.Root.Device - if upnp := getRouter(dev, rootDevice, toClient); upnp != nil { - return upnp - } - - // attempt to connect to any sub devices - devices := rootDevice.Devices - for i := range devices { - if upnp := getRouter(dev, &devices[i], toClient); upnp != nil { - return upnp - } + for i := 0; i < len(devs); i++ { + if r := <-router; r != nil { + return r } } + return nil } -func getRouter(rootDevice *goupnp.MaybeRootDevice, device *goupnp.Device, toClient func(goupnp.ServiceClient) upnpClient) *upnpRouter { - for i := range device.Services { - service := &device.Services[i] +// getUPnPRouter searches for internet gateway using both Device Control Protocol +// and returns the first one it can find. It returns nil if no UPnP gateway is found +func getUPnPRouter() *upnpRouter { + targets := []string{ + internetgateway1.URN_WANConnectionDevice_1, + internetgateway2.URN_WANConnectionDevice_2, + } - soapClient := service.NewSOAPClient() - // make sure the client times out if needed - soapClient.HTTPClient.Timeout = soapTimeout + routers := make(chan *upnpRouter) - // attempt to create a client connection - serviceClient := goupnp.ServiceClient{ - SOAPClient: soapClient, - RootDevice: rootDevice.Root, - Location: rootDevice.Location, - Service: service, - } - client := toClient(serviceClient) - if client == nil { - continue - } + for _, urn := range targets { + go func(urn string) { + routers <- discover(urn) + }(urn) + } - // check whether port mapping is enabled - if _, nat, err := client.GetNATRSIPStatus(); err != nil || !nat { - continue - } - - // we found a router! - return &upnpRouter{ - root: rootDevice.Root, - client: client, + for i := 0; i < len(targets); i++ { + if r := <-routers; r != nil { + return r } } + return nil } diff --git a/network/builder.go b/network/builder.go index 34feb99..66affc0 100644 --- a/network/builder.go +++ b/network/builder.go @@ -33,6 +33,12 @@ func (m Builder) PeerList(ipDescs []utils.IPDesc) (Msg, error) { return m.Pack(PeerList, map[Field]interface{}{Peers: ipDescs}) } +// Ping message +func (m Builder) Ping() (Msg, error) { return m.Pack(Ping, nil) } + +// Pong message +func (m Builder) Pong() (Msg, error) { return m.Pack(Pong, nil) } + // GetAcceptedFrontier message func (m Builder) GetAcceptedFrontier(chainID ids.ID, requestID uint32) (Msg, error) { return m.Pack(GetAcceptedFrontier, map[Field]interface{}{ diff --git a/network/commands.go b/network/commands.go index c5feb89..06fc31b 100644 --- a/network/commands.go +++ b/network/commands.go @@ -132,6 +132,10 @@ func (op Op) String() string { return "get_peerlist" case PeerList: return "peerlist" + case Ping: + return "ping" + case Pong: + return "pong" case GetAcceptedFrontier: return "get_accepted_frontier" case AcceptedFrontier: @@ -177,11 +181,12 @@ const ( PushQuery PullQuery Chits - // Bootstrapping: - // TODO: Move GetAncestors and MultiPut with the rest of the bootstrapping - // commands when we do non-backwards compatible upgrade + + // TODO: Reorder these messages when we transition to everest GetAncestors MultiPut + Ping + Pong ) // Defines the messages that can be sent/received with this network @@ -192,6 +197,8 @@ var ( Version: {NetworkID, NodeID, MyTime, IP, VersionStr}, GetPeerList: {}, PeerList: {Peers}, + Ping: {}, + Pong: {}, // Bootstrapping: GetAcceptedFrontier: {ChainID, RequestID}, AcceptedFrontier: {ChainID, RequestID, ContainerIDs}, diff --git a/network/metrics.go b/network/metrics.go index 35c9410..f8abd89 100644 --- a/network/metrics.go +++ b/network/metrics.go @@ -54,6 +54,7 @@ type metrics struct { getVersion, version, getPeerlist, peerlist, + ping, pong, getAcceptedFrontier, acceptedFrontier, getAccepted, accepted, get, getAncestors, put, multiPut, @@ -78,6 +79,8 @@ func (m *metrics) initialize(registerer prometheus.Registerer) error { errs.Add(m.version.initialize(Version, registerer)) errs.Add(m.getPeerlist.initialize(GetPeerList, registerer)) errs.Add(m.peerlist.initialize(PeerList, registerer)) + errs.Add(m.ping.initialize(Ping, registerer)) + errs.Add(m.pong.initialize(Pong, registerer)) errs.Add(m.getAcceptedFrontier.initialize(GetAcceptedFrontier, registerer)) errs.Add(m.acceptedFrontier.initialize(AcceptedFrontier, registerer)) errs.Add(m.getAccepted.initialize(GetAccepted, registerer)) @@ -103,6 +106,10 @@ func (m *metrics) message(msgType Op) *messageMetrics { return &m.getPeerlist case PeerList: return &m.peerlist + case Ping: + return &m.ping + case Pong: + return &m.pong case GetAcceptedFrontier: return &m.getAcceptedFrontier case AcceptedFrontier: diff --git a/network/network.go b/network/network.go index 0300b09..bce1216 100644 --- a/network/network.go +++ b/network/network.go @@ -21,6 +21,7 @@ import ( "github.com/ava-labs/gecko/snow/triggers" "github.com/ava-labs/gecko/snow/validators" "github.com/ava-labs/gecko/utils" + "github.com/ava-labs/gecko/utils/formatting" "github.com/ava-labs/gecko/utils/logging" "github.com/ava-labs/gecko/utils/random" "github.com/ava-labs/gecko/utils/timer" @@ -42,6 +43,12 @@ const ( defaultGetVersionTimeout = 2 * time.Second defaultAllowPrivateIPs = true defaultGossipSize = 50 + defaultPingPongTimeout = time.Minute + defaultPingFrequency = 3 * defaultPingPongTimeout / 4 + + // Request ID used when sending a Put message to gossip an accepted container + // (ie not sent in response to a Get) + GossipMsgRequestID = math.MaxUint32 ) // Network defines the functionality of the networking library. @@ -98,6 +105,7 @@ type network struct { serverUpgrader Upgrader clientUpgrader Upgrader vdrs validators.Set // set of current validators in the AVAnet + beacons validators.Set // set of beacons in the AVAnet router router.Router // router must be thread safe nodeID uint32 @@ -118,6 +126,8 @@ type network struct { getVersionTimeout time.Duration allowPrivateIPs bool gossipSize int + pingPongTimeout time.Duration + pingFrequency time.Duration executor timer.Executor @@ -150,6 +160,7 @@ func NewDefaultNetwork( serverUpgrader, clientUpgrader Upgrader, vdrs validators.Set, + beacons validators.Set, router router.Router, ) Network { return NewNetwork( @@ -165,6 +176,7 @@ func NewDefaultNetwork( serverUpgrader, clientUpgrader, vdrs, + beacons, router, defaultInitialReconnectDelay, defaultMaxReconnectDelay, @@ -179,6 +191,8 @@ func NewDefaultNetwork( defaultGetVersionTimeout, defaultAllowPrivateIPs, defaultGossipSize, + defaultPingPongTimeout, + defaultPingFrequency, ) } @@ -196,6 +210,7 @@ func NewNetwork( serverUpgrader, clientUpgrader Upgrader, vdrs validators.Set, + beacons validators.Set, router router.Router, initialReconnectDelay, maxReconnectDelay time.Duration, @@ -210,6 +225,8 @@ func NewNetwork( getVersionTimeout time.Duration, allowPrivateIPs bool, gossipSize int, + pingPongTimeout time.Duration, + pingFrequency time.Duration, ) Network { net := &network{ log: log, @@ -223,6 +240,7 @@ func NewNetwork( serverUpgrader: serverUpgrader, clientUpgrader: clientUpgrader, vdrs: vdrs, + beacons: beacons, router: router, nodeID: rand.Uint32(), initialReconnectDelay: initialReconnectDelay, @@ -238,6 +256,8 @@ func NewNetwork( getVersionTimeout: getVersionTimeout, allowPrivateIPs: allowPrivateIPs, gossipSize: gossipSize, + pingPongTimeout: pingPongTimeout, + pingFrequency: pingFrequency, disconnectedIPs: make(map[string]struct{}), connectedIPs: make(map[string]struct{}), @@ -278,8 +298,11 @@ func (n *network) GetAcceptedFrontier(validatorIDs ids.ShortSet, chainID ids.ID, func (n *network) AcceptedFrontier(validatorID ids.ShortID, chainID ids.ID, requestID uint32, containerIDs ids.Set) { msg, err := n.b.AcceptedFrontier(chainID, requestID, containerIDs) if err != nil { - n.log.Error("attempted to pack too large of an AcceptedFrontier message.\nNumber of containerIDs: %d", - containerIDs.Len()) + n.log.Error("failed to build AcceptedFrontier(%s, %d, %s): %s", + chainID, + requestID, + containerIDs, + err) return // Packing message failed } @@ -291,7 +314,11 @@ func (n *network) AcceptedFrontier(validatorID ids.ShortID, chainID ids.ID, requ sent = peer.send(msg) } if !sent { - n.log.Debug("failed to send an AcceptedFrontier message to: %s", validatorID) + n.log.Debug("failed to send AcceptedFrontier(%s, %s, %d, %s)", + validatorID, + chainID, + requestID, + containerIDs) n.acceptedFrontier.numFailed.Inc() } else { n.acceptedFrontier.numSent.Inc() @@ -302,6 +329,11 @@ func (n *network) AcceptedFrontier(validatorID ids.ShortID, chainID ids.ID, requ func (n *network) GetAccepted(validatorIDs ids.ShortSet, chainID ids.ID, requestID uint32, containerIDs ids.Set) { msg, err := n.b.GetAccepted(chainID, requestID, containerIDs) if err != nil { + n.log.Error("failed to build GetAccepted(%s, %d, %s): %s", + chainID, + requestID, + containerIDs, + err) for _, validatorID := range validatorIDs.List() { vID := validatorID n.executor.Add(func() { n.router.GetAcceptedFailed(vID, chainID, requestID) }) @@ -319,6 +351,11 @@ func (n *network) GetAccepted(validatorIDs ids.ShortSet, chainID ids.ID, request sent = peer.send(msg) } if !sent { + n.log.Debug("failed to send GetAccepted(%s, %s, %d, %s)", + validatorID, + chainID, + requestID, + containerIDs) n.executor.Add(func() { n.router.GetAcceptedFailed(vID, chainID, requestID) }) n.getAccepted.numFailed.Inc() } else { @@ -331,8 +368,11 @@ func (n *network) GetAccepted(validatorIDs ids.ShortSet, chainID ids.ID, request func (n *network) Accepted(validatorID ids.ShortID, chainID ids.ID, requestID uint32, containerIDs ids.Set) { msg, err := n.b.Accepted(chainID, requestID, containerIDs) if err != nil { - n.log.Error("attempted to pack too large of an Accepted message.\nNumber of containerIDs: %d", - containerIDs.Len()) + n.log.Error("failed to build Accepted(%s, %d, %s): %s", + chainID, + requestID, + containerIDs, + err) return // Packing message failed } @@ -344,33 +384,17 @@ func (n *network) Accepted(validatorID ids.ShortID, chainID ids.ID, requestID ui sent = peer.send(msg) } if !sent { - n.log.Debug("failed to send an Accepted message to: %s", validatorID) + n.log.Debug("failed to send Accepted(%s, %s, %d, %s)", + validatorID, + chainID, + requestID, + containerIDs) n.accepted.numFailed.Inc() } else { n.accepted.numSent.Inc() } } -// Get implements the Sender interface. -func (n *network) Get(validatorID ids.ShortID, chainID ids.ID, requestID uint32, containerID ids.ID) { - msg, err := n.b.Get(chainID, requestID, containerID) - n.log.AssertNoError(err) - - n.stateLock.Lock() - defer n.stateLock.Unlock() - - peer, sent := n.peers[validatorID.Key()] - if sent { - sent = peer.send(msg) - } - if !sent { - n.log.Debug("failed to send a Get message to: %s", validatorID) - n.get.numFailed.Inc() - } else { - n.get.numSent.Inc() - } -} - // GetAncestors implements the Sender interface. func (n *network) GetAncestors(validatorID ids.ShortID, chainID ids.ID, requestID uint32, containerID ids.ID) { msg, err := n.b.GetAncestors(chainID, requestID, containerID) @@ -387,36 +411,18 @@ func (n *network) GetAncestors(validatorID ids.ShortID, chainID ids.ID, requestI sent = peer.send(msg) } if !sent { + n.log.Debug("failed to send GetAncestors(%s, %s, %d, %s)", + validatorID, + chainID, + requestID, + containerID) + n.executor.Add(func() { n.router.GetAncestorsFailed(validatorID, chainID, requestID) }) n.getAncestors.numFailed.Inc() - n.log.Debug("failed to send a GetAncestors message to: %s", validatorID) } else { n.getAncestors.numSent.Inc() } } -// Put implements the Sender interface. -func (n *network) Put(validatorID ids.ShortID, chainID ids.ID, requestID uint32, containerID ids.ID, container []byte) { - msg, err := n.b.Put(chainID, requestID, containerID, container) - if err != nil { - n.log.Error("failed to build Put message because of container of size %d", len(container)) - return - } - - n.stateLock.Lock() - defer n.stateLock.Unlock() - - peer, sent := n.peers[validatorID.Key()] - if sent { - sent = peer.send(msg) - } - if !sent { - n.log.Debug("failed to send a Put message to: %s", validatorID) - n.put.numFailed.Inc() - } else { - n.put.numSent.Inc() - } -} - // MultiPut implements the Sender interface. func (n *network) MultiPut(validatorID ids.ShortID, chainID ids.ID, requestID uint32, containers [][]byte) { msg, err := n.b.MultiPut(chainID, requestID, containers) @@ -433,22 +439,90 @@ func (n *network) MultiPut(validatorID ids.ShortID, chainID ids.ID, requestID ui sent = peer.send(msg) } if !sent { - n.log.Debug("failed to send a MultiPut message to: %s", validatorID) + n.log.Debug("failed to send MultiPut(%s, %s, %d, %d)", + validatorID, + chainID, + requestID, + len(containers)) n.multiPut.numFailed.Inc() } else { n.multiPut.numSent.Inc() } } +// Get implements the Sender interface. +func (n *network) Get(validatorID ids.ShortID, chainID ids.ID, requestID uint32, containerID ids.ID) { + msg, err := n.b.Get(chainID, requestID, containerID) + n.log.AssertNoError(err) + + n.stateLock.Lock() + defer n.stateLock.Unlock() + + peer, sent := n.peers[validatorID.Key()] + if sent { + sent = peer.send(msg) + } + if !sent { + n.log.Debug("failed to send Get(%s, %s, %d, %s)", + validatorID, + chainID, + requestID, + containerID) + n.executor.Add(func() { n.router.GetFailed(validatorID, chainID, requestID) }) + n.get.numFailed.Inc() + } else { + n.get.numSent.Inc() + } +} + +// Put implements the Sender interface. +func (n *network) Put(validatorID ids.ShortID, chainID ids.ID, requestID uint32, containerID ids.ID, container []byte) { + msg, err := n.b.Put(chainID, requestID, containerID, container) + if err != nil { + n.log.Error("failed to build Put(%s, %d, %s): %s. len(container) : %d", + chainID, + requestID, + containerID, + err, + len(container)) + return + } + + n.stateLock.Lock() + defer n.stateLock.Unlock() + + peer, sent := n.peers[validatorID.Key()] + if sent { + sent = peer.send(msg) + } + if !sent { + n.log.Debug("failed to send Put(%s, %s, %d, %s)", + validatorID, + chainID, + requestID, + containerID) + n.log.Verbo("container: %s", formatting.DumpBytes{Bytes: container}) + n.put.numFailed.Inc() + } else { + n.put.numSent.Inc() + } +} + // PushQuery implements the Sender interface. func (n *network) PushQuery(validatorIDs ids.ShortSet, chainID ids.ID, requestID uint32, containerID ids.ID, container []byte) { msg, err := n.b.PushQuery(chainID, requestID, containerID, container) if err != nil { + n.log.Error("failed to build PushQuery(%s, %d, %s): %s. len(container): %d", + chainID, + requestID, + containerID, + err, + len(container)) + n.log.Verbo("container: %s", formatting.DumpBytes{Bytes: container}) for _, validatorID := range validatorIDs.List() { vID := validatorID n.executor.Add(func() { n.router.QueryFailed(vID, chainID, requestID) }) } - n.log.Error("attempted to pack too large of a PushQuery message.\nContainer length: %d", len(container)) return // Packing message failed } @@ -462,7 +536,12 @@ func (n *network) PushQuery(validatorIDs ids.ShortSet, chainID ids.ID, requestID sent = peer.send(msg) } if !sent { - n.log.Debug("failed sending a PushQuery message to: %s", vID) + n.log.Debug("failed to send PushQuery(%s, %s, %d, %s)", + validatorID, + chainID, + requestID, + containerID) + n.log.Verbo("container: %s", formatting.DumpBytes{Bytes: container}) n.executor.Add(func() { n.router.QueryFailed(vID, chainID, requestID) }) n.pushQuery.numFailed.Inc() } else { @@ -486,7 +565,11 @@ func (n *network) PullQuery(validatorIDs ids.ShortSet, chainID ids.ID, requestID sent = peer.send(msg) } if !sent { - n.log.Debug("failed sending a PullQuery message to: %s", vID) + n.log.Debug("failed to send PullQuery(%s, %s, %d, %s)", + validatorID, + chainID, + requestID, + containerID) n.executor.Add(func() { n.router.QueryFailed(vID, chainID, requestID) }) n.pullQuery.numFailed.Inc() } else { @@ -499,7 +582,11 @@ func (n *network) PullQuery(validatorIDs ids.ShortSet, chainID ids.ID, requestID func (n *network) Chits(validatorID ids.ShortID, chainID ids.ID, requestID uint32, votes ids.Set) { msg, err := n.b.Chits(chainID, requestID, votes) if err != nil { - n.log.Error("failed to build Chits message because of %d votes", votes.Len()) + n.log.Error("failed to build Chits(%s, %d, %s): %s", + chainID, + requestID, + votes, + err) return } @@ -511,7 +598,11 @@ func (n *network) Chits(validatorID ids.ShortID, chainID ids.ID, requestID uint3 sent = peer.send(msg) } if !sent { - n.log.Debug("failed to send a Chits message to: %s", validatorID) + n.log.Debug("failed to send Chits(%s, %s, %d, %s)", + validatorID, + chainID, + requestID, + votes) n.chits.numFailed.Inc() } else { n.chits.numSent.Inc() @@ -521,7 +612,8 @@ func (n *network) Chits(validatorID ids.ShortID, chainID ids.ID, requestID uint3 // Gossip attempts to gossip the container to the network func (n *network) Gossip(chainID, containerID ids.ID, container []byte) { if err := n.gossipContainer(chainID, containerID, container); err != nil { - n.log.Error("error gossiping container %s to %s: %s", containerID, chainID, err) + n.log.Debug("failed to Gossip(%s, %s): %s", chainID, containerID, err) + n.log.Verbo("container:\n%s", formatting.DumpBytes{Bytes: container}) } } @@ -632,7 +724,7 @@ func (n *network) Track(ip utils.IPDesc) { // assumes the stateLock is not held. func (n *network) gossipContainer(chainID, containerID ids.ID, container []byte) error { - msg, err := n.b.Put(chainID, math.MaxUint32, containerID, container) + msg, err := n.b.Put(chainID, GossipMsgRequestID, containerID, container) if err != nil { return fmt.Errorf("attempted to pack too large of a Put message.\nContainer length: %d", len(container)) } @@ -695,7 +787,9 @@ func (n *network) gossip() { } msg, err := n.b.PeerList(ips) if err != nil { - n.log.Warn("failed to gossip PeerList message due to %s", err) + n.log.Error("failed to build peer list to gossip: %s. len(ips): %d", + err, + len(ips)) continue } diff --git a/network/network_test.go b/network/network_test.go index 0fd7053..c230402 100644 --- a/network/network_test.go +++ b/network/network_test.go @@ -197,6 +197,7 @@ func TestNewDefaultNetwork(t *testing.T) { serverUpgrader, clientUpgrader, vdrs, + vdrs, handler, ) assert.NotNil(t, net) @@ -280,6 +281,7 @@ func TestEstablishConnection(t *testing.T) { serverUpgrader, clientUpgrader, vdrs, + vdrs, handler, ) assert.NotNil(t, net0) @@ -297,6 +299,7 @@ func TestEstablishConnection(t *testing.T) { serverUpgrader, clientUpgrader, vdrs, + vdrs, handler, ) assert.NotNil(t, net1) @@ -419,6 +422,7 @@ func TestDoubleTrack(t *testing.T) { serverUpgrader, clientUpgrader, vdrs, + vdrs, handler, ) assert.NotNil(t, net0) @@ -436,6 +440,7 @@ func TestDoubleTrack(t *testing.T) { serverUpgrader, clientUpgrader, vdrs, + vdrs, handler, ) assert.NotNil(t, net1) @@ -559,6 +564,7 @@ func TestDoubleClose(t *testing.T) { serverUpgrader, clientUpgrader, vdrs, + vdrs, handler, ) assert.NotNil(t, net0) @@ -576,6 +582,7 @@ func TestDoubleClose(t *testing.T) { serverUpgrader, clientUpgrader, vdrs, + vdrs, handler, ) assert.NotNil(t, net1) @@ -704,6 +711,7 @@ func TestRemoveHandlers(t *testing.T) { serverUpgrader, clientUpgrader, vdrs, + vdrs, handler, ) assert.NotNil(t, net0) @@ -721,6 +729,7 @@ func TestRemoveHandlers(t *testing.T) { serverUpgrader, clientUpgrader, vdrs, + vdrs, handler, ) assert.NotNil(t, net1) @@ -858,6 +867,7 @@ func TestTrackConnected(t *testing.T) { serverUpgrader, clientUpgrader, vdrs, + vdrs, handler, ) assert.NotNil(t, net0) @@ -875,6 +885,7 @@ func TestTrackConnected(t *testing.T) { serverUpgrader, clientUpgrader, vdrs, + vdrs, handler, ) assert.NotNil(t, net1) @@ -999,6 +1010,7 @@ func TestTrackConnectedRace(t *testing.T) { serverUpgrader, clientUpgrader, vdrs, + vdrs, handler, ) assert.NotNil(t, net0) @@ -1016,6 +1028,7 @@ func TestTrackConnectedRace(t *testing.T) { serverUpgrader, clientUpgrader, vdrs, + vdrs, handler, ) assert.NotNil(t, net1) diff --git a/network/peer.go b/network/peer.go index 980617c..cf2f37f 100644 --- a/network/peer.go +++ b/network/peer.go @@ -64,6 +64,24 @@ func (p *peer) Start() { // Initially send the version to the peer go p.Version() go p.requestVersion() + // go p.sendPings() +} + +func (p *peer) sendPings() { + t := time.NewTicker(p.net.pingFrequency) + defer t.Stop() + + for range t.C { + p.net.stateLock.Lock() + closed := p.closed + p.net.stateLock.Unlock() + + if closed { + return + } + + p.Ping() + } } // request the version from the peer until we get the version from them @@ -80,6 +98,7 @@ func (p *peer) requestVersion() { if connected || closed { return } + p.GetVersion() } } @@ -88,6 +107,11 @@ func (p *peer) requestVersion() { func (p *peer) ReadMessages() { defer p.Close() + // if err := p.conn.SetReadDeadline(p.net.clock.Time().Add(p.net.pingPongTimeout)); err != nil { + // p.net.log.Verbo("error on setting the connection read timeout %s", err) + // return + // } + pendingBuffer := wrappers.Packer{} readBuffer := make([]byte, 1<<10) for { @@ -218,7 +242,15 @@ func (p *peer) send(msg Msg) bool { // assumes the stateLock is not held func (p *peer) handle(msg Msg) { p.net.heartbeat() - atomic.StoreInt64(&p.lastReceived, p.net.clock.Time().Unix()) + + currentTime := p.net.clock.Time() + atomic.StoreInt64(&p.lastReceived, currentTime.Unix()) + + // if err := p.conn.SetReadDeadline(currentTime.Add(p.net.pingPongTimeout)); err != nil { + // p.net.log.Verbo("error on setting the connection read timeout %s, closing the connection", err) + // p.Close() + // return + // } op := msg.Op() msgMetrics := p.net.message(op) @@ -235,6 +267,12 @@ func (p *peer) handle(msg Msg) { case GetVersion: p.getVersion(msg) return + case Ping: + p.ping(msg) + return + case Pong: + p.pong(msg) + return } if !p.connected { p.net.log.Debug("dropping message from %s because the connection hasn't been established yet", p.id) @@ -318,6 +356,12 @@ func (p *peer) GetPeerList() { p.Send(msg) } +// assumes the stateLock is not held +func (p *peer) SendPeerList() { + ips := p.net.validatorIPs() + p.PeerList(ips) +} + // assumes the stateLock is not held func (p *peer) PeerList(peers []utils.IPDesc) { msg, err := p.net.b.PeerList(peers) @@ -326,7 +370,28 @@ func (p *peer) PeerList(peers []utils.IPDesc) { return } p.Send(msg) - return +} + +// assumes the stateLock is not held +func (p *peer) Ping() { + msg, err := p.net.b.Ping() + p.net.log.AssertNoError(err) + if p.Send(msg) { + p.net.ping.numSent.Inc() + } else { + p.net.ping.numFailed.Inc() + } +} + +// assumes the stateLock is not held +func (p *peer) Pong() { + msg, err := p.net.b.Pong() + p.net.log.AssertNoError(err) + if p.Send(msg) { + p.net.pong.numSent.Inc() + } else { + p.net.pong.numFailed.Inc() + } } // assumes the stateLock is not held @@ -405,8 +470,13 @@ func (p *peer) version(msg Msg) { } if p.net.version.Before(peerVersion) { - p.net.log.Info("peer attempting to connect with newer version %s. You may want to update your client", - peerVersion) + if p.net.beacons.Contains(p.id) { + p.net.log.Info("beacon attempting to connect with newer version %s. You may want to update your client", + peerVersion) + } else { + p.net.log.Debug("peer attempting to connect with newer version %s. You may want to update your client", + peerVersion) + } } if err := p.net.version.Compatible(peerVersion); err != nil { @@ -458,17 +528,6 @@ func (p *peer) version(msg Msg) { p.net.connected(p) } -// assumes the stateLock is not held -func (p *peer) SendPeerList() { - ips := p.net.validatorIPs() - reply, err := p.net.b.PeerList(ips) - if err != nil { - p.net.log.Warn("failed to send PeerList message due to %s", err) - return - } - p.Send(reply) -} - // assumes the stateLock is not held func (p *peer) getPeerList(_ Msg) { p.SendPeerList() } @@ -488,6 +547,12 @@ func (p *peer) peerList(msg Msg) { p.net.stateLock.Unlock() } +// assumes the stateLock is not held +func (p *peer) ping(_ Msg) { p.Pong() } + +// assumes the stateLock is not held +func (p *peer) pong(_ Msg) {} + // assumes the stateLock is not held func (p *peer) getAcceptedFrontier(msg Msg) { chainID, err := ids.ToID(msg.Get(ChainID).([]byte)) diff --git a/node/config.go b/node/config.go index 74ff491..e578fc4 100644 --- a/node/config.go +++ b/node/config.go @@ -33,10 +33,12 @@ type Config struct { DB database.Database // Staking configuration - StakingIP utils.IPDesc - EnableStaking bool - StakingKeyFile string - StakingCertFile string + StakingIP utils.IPDesc + StakingLocalPort uint16 + EnableP2PTLS bool + EnableStaking bool + StakingKeyFile string + StakingCertFile string // Bootstrapping configuration BootstrapPeers []*Peer @@ -50,6 +52,7 @@ type Config struct { // Enable/Disable APIs AdminAPIEnabled bool + InfoAPIEnabled bool KeystoreAPIEnabled bool MetricsAPIEnabled bool HealthAPIEnabled bool diff --git a/node/node.go b/node/node.go index ea0e8fc..ea40c77 100644 --- a/node/node.go +++ b/node/node.go @@ -7,6 +7,7 @@ import ( "crypto/tls" "crypto/x509" "encoding/pem" + "errors" "fmt" "io/ioutil" "net" @@ -18,6 +19,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" @@ -56,7 +58,7 @@ var ( genesisHashKey = []byte("genesisID") // Version is the version of this code - Version = version.NewDefaultVersion("avalanche", 0, 5, 5) + Version = version.NewDefaultVersion("avalanche", 0, 5, 7) versionParser = version.NewDefaultParser() ) @@ -92,6 +94,9 @@ type Node struct { // Net runs the networking stack Net network.Network + // this node's initial connections to the network + beacons validators.Set + // current validators of the network vdrs validators.Manager @@ -112,14 +117,14 @@ type Node struct { */ func (n *Node) initNetworking() error { - listener, err := net.Listen(TCP, n.Config.StakingIP.PortString()) + listener, err := net.Listen(TCP, fmt.Sprintf(":%d", n.Config.StakingLocalPort)) if err != nil { return err } dialer := network.NewDialer(TCP) var serverUpgrader, clientUpgrader network.Upgrader - if n.Config.EnableStaking { + if n.Config.EnableP2PTLS { cert, err := tls.LoadX509KeyPair(n.Config.StakingCertFile, n.Config.StakingKeyFile) if err != nil { return err @@ -164,6 +169,7 @@ func (n *Node) initNetworking() error { serverUpgrader, clientUpgrader, defaultSubnetValidators, + n.beacons, n.Config.ConsensusRouter, ) @@ -253,7 +259,7 @@ func (n *Node) initDatabase() error { // Otherwise, it is a hash of the TLS certificate that this node // uses for P2P communication func (n *Node) initNodeID() error { - if !n.Config.EnableStaking { + if !n.Config.EnableP2PTLS { n.ID = ids.NewShortID(hashing.ComputeHash160Array([]byte(n.Config.StakingIP.String()))) n.Log.Info("Set the node's ID to %s", n.ID) return nil @@ -277,6 +283,14 @@ func (n *Node) initNodeID() error { return nil } +// Create the IDs of the peers this node should first connect to +func (n *Node) initBeacons() { + n.beacons = validators.NewSet() + for _, peer := range n.Config.BootstrapPeers { + n.beacons.Add(validators.NewValidator(peer.ID, 1)) + } +} + // Create the vmManager and register the following vms: // AVM, Simple Payments DAG, Simple Payments Chain // The Platform VM is registered in initStaking because @@ -359,11 +373,6 @@ func (n *Node) initChains() error { return err } - beacons := validators.NewSet() - for _, peer := range n.Config.BootstrapPeers { - beacons.Add(validators.NewValidator(peer.ID, 1)) - } - genesisBytes, err := genesis.Genesis(n.Config.NetworkID) if err != nil { return err @@ -375,7 +384,7 @@ func (n *Node) initChains() error { SubnetID: platformvm.DefaultSubnetID, GenesisData: genesisBytes, // Specifies other chains to create VMAlias: platformvm.ID.String(), - CustomBeacons: beacons, + CustomBeacons: n.beacons, }) return nil @@ -435,58 +444,105 @@ func (n *Node) initSharedMemory() { // initKeystoreAPI initializes the keystore service // Assumes n.APIServer is already set -func (n *Node) initKeystoreAPI() { - n.Log.Info("initializing Keystore API") +func (n *Node) initKeystoreAPI() error { + n.Log.Info("initializing keystore") keystoreDB := prefixdb.New([]byte("keystore"), n.DB) n.keystoreServer.Initialize(n.Log, keystoreDB) keystoreHandler := n.keystoreServer.CreateHandler() - if n.Config.KeystoreAPIEnabled { - n.APIServer.AddRoute(keystoreHandler, &sync.RWMutex{}, "keystore", "", n.HTTPLog) + if !n.Config.KeystoreAPIEnabled { + n.Log.Info("skipping keystore API initializaion because it has been disabled") + return nil } + n.Log.Info("initializing keystore API") + return n.APIServer.AddRoute(keystoreHandler, &sync.RWMutex{}, "keystore", "", n.HTTPLog) + } // initMetricsAPI initializes the Metrics API // Assumes n.APIServer is already set -func (n *Node) initMetricsAPI() { - n.Log.Info("initializing Metrics API") +func (n *Node) initMetricsAPI() error { registry, handler := metrics.NewService() - if n.Config.MetricsAPIEnabled { - n.APIServer.AddRoute(handler, &sync.RWMutex{}, "metrics", "", n.HTTPLog) - } + // It is assumed by components of the system that the Metrics interface is + // non-nil. So, it is set regardless of if the metrics API is available or not. n.Config.ConsensusParams.Metrics = registry + if !n.Config.MetricsAPIEnabled { + n.Log.Info("skipping metrics API initialization because it has been disabled") + return nil + } + n.Log.Info("initializing metrics API") + return n.APIServer.AddRoute(handler, &sync.RWMutex{}, "metrics", "", n.HTTPLog) } // initAdminAPI initializes the Admin API service // Assumes n.log, n.chainManager, and n.ValidatorAPI already initialized -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) - n.APIServer.AddRoute(service, &sync.RWMutex{}, "admin", "", n.HTTPLog) +func (n *Node) initAdminAPI() error { + if !n.Config.AdminAPIEnabled { + n.Log.Info("skipping admin API initializaion because it has been disabled") + return nil } + n.Log.Info("initializing admin API") + service := admin.NewService(Version, n.ID, n.Config.NetworkID, n.Log, n.chainManager, n.Net, &n.APIServer) + return n.APIServer.AddRoute(service, &sync.RWMutex{}, "admin", "", n.HTTPLog) +} + +func (n *Node) initInfoAPI() error { + if !n.Config.InfoAPIEnabled { + n.Log.Info("skipping info API initializaion because it has been disabled") + return nil + } + n.Log.Info("initializing info API") + service := info.NewService(n.Log, Version, n.ID, n.Config.NetworkID, n.chainManager, n.Net) + return 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() { +// Assumes n.Log, n.Net, n.APIServer, n.HTTPLog already initialized +func (n *Node) initHealthAPI() error { if !n.Config.HealthAPIEnabled { - return + n.Log.Info("skipping health API initializaion because it has been disabled") + return nil } - n.Log.Info("initializing Health API") service := health.NewService(n.Log) - service.RegisterHeartbeat("network.validators.heartbeat", n.Net, 5*time.Minute) - n.APIServer.AddRoute(service.Handler(), &sync.RWMutex{}, "health", "", n.HTTPLog) + if err := service.RegisterHeartbeat("network.validators.heartbeat", n.Net, 5*time.Minute); err != nil { + return fmt.Errorf("couldn't register heartbeat health check: %w", err) + } + isBootstrappedFunc := func() (interface{}, error) { + if pChainID, err := n.chainManager.Lookup("P"); err != nil { + return nil, errors.New("P-Chain not created") + } else if !n.chainManager.IsBootstrapped(pChainID) { + return nil, errors.New("P-Chain not bootstrapped") + } + if xChainID, err := n.chainManager.Lookup("X"); err != nil { + return nil, errors.New("X-Chain not created") + } else if !n.chainManager.IsBootstrapped(xChainID) { + return nil, errors.New("X-Chain not bootstrapped") + } + if cChainID, err := n.chainManager.Lookup("C"); err != nil { + return nil, errors.New("C-Chain not created") + } else if !n.chainManager.IsBootstrapped(cChainID) { + return nil, errors.New("C-Chain not bootstrapped") + } + return nil, nil + } + // Passes if the P, X and C chains are finished bootstrapping + if err := service.RegisterMonotonicCheckFunc("chains.default.bootstrapped", isBootstrappedFunc); err != nil { + return err + } + return n.APIServer.AddRoute(service.Handler(), &sync.RWMutex{}, "health", "", n.HTTPLog) } // initIPCAPI initializes the IPC API service // Assumes n.log and n.chainManager already initialized -func (n *Node) initIPCAPI() { - if n.Config.IPCEnabled { - n.Log.Info("initializing IPC API") - service := ipcs.NewService(n.Log, n.chainManager, n.DecisionDispatcher, &n.APIServer) - n.APIServer.AddRoute(service, &sync.RWMutex{}, "ipcs", "", n.HTTPLog) +func (n *Node) initIPCAPI() error { + if !n.Config.IPCEnabled { + n.Log.Info("skipping ipc API initializaion because it has been disabled") + return nil } + n.Log.Info("initializing ipc API") + service := ipcs.NewService(n.Log, n.chainManager, n.DecisionDispatcher, &n.APIServer) + return n.APIServer.AddRoute(service, &sync.RWMutex{}, "ipcs", "", n.HTTPLog) } // Give chains and VMs aliases as specified by the genesis information @@ -542,10 +598,16 @@ func (n *Node) Initialize(Config *Config, logger logging.Logger, logFactory logg return fmt.Errorf("problem initializing staker ID: %w", err) } + n.initBeacons() + // Start HTTP APIs - n.initAPIServer() // Start the API Server - n.initKeystoreAPI() // Start the Keystore API - n.initMetricsAPI() // Start the Metrics API + n.initAPIServer() // Start the API Server + if err := n.initKeystoreAPI(); err != nil { // Start the Keystore API + return fmt.Errorf("couldn't initialize keystore API: %w", err) + } + if err := n.initMetricsAPI(); err != nil { // Start the Metrics API + return fmt.Errorf("couldn't initialize metrics API: %w", err) + } // initialize shared memory n.initSharedMemory() @@ -561,14 +623,25 @@ func (n *Node) Initialize(Config *Config, logger logging.Logger, logFactory logg n.initEventDispatcher() // Set up the event dipatcher n.initChainManager() // Set up the chain manager - n.initAdminAPI() // Start the Admin API - n.initHealthAPI() // Start the Health API - n.initIPCAPI() // Start the IPC API - - if err := n.initAliases(); err != nil { // Set up aliases - return err + if err := n.initAdminAPI(); err != nil { // Start the Admin API + return fmt.Errorf("couldn't initialize admin API: %w", err) } - return n.initChains() // Start the Platform chain + if err := n.initInfoAPI(); err != nil { // Start the Info API + return fmt.Errorf("couldn't initialize info API: %w", err) + } + if err := n.initHealthAPI(); err != nil { // Start the Health API + return fmt.Errorf("couldn't initialize health API: %w", err) + } + if err := n.initIPCAPI(); err != nil { // Start the IPC API + return fmt.Errorf("couldn't initialize ipc API: %w", err) + } + if err := n.initAliases(); err != nil { // Set up aliases + return fmt.Errorf("couldn't initialize aliases: %w", err) + } + if err := n.initChains(); err != nil { // Start the Platform chain + return fmt.Errorf("couldn't initialize chains: %w", err) + } + return nil } // Shutdown this node diff --git a/scripts/build.sh b/scripts/build.sh index 9bee1cf..a0a59d6 100755 --- a/scripts/build.sh +++ b/scripts/build.sh @@ -15,7 +15,7 @@ GECKO_PATH=$( cd "$( dirname "${BASH_SOURCE[0]}" )"; cd .. && pwd ) # Directory BUILD_DIR=$GECKO_PATH/build # Where binaries go PLUGIN_DIR="$BUILD_DIR/plugins" # Where plugin binaries (namely coreth) go -CORETH_VER="0.2.4" # Should match coreth version in go.mod +CORETH_VER="0.2.5" # Should match coreth version in go.mod CORETH_PATH="$GOPATH/pkg/mod/github.com/ava-labs/coreth@v$CORETH_VER" # Build Gecko diff --git a/snow/consensus/avalanche/consensus_test.go b/snow/consensus/avalanche/consensus_test.go index 3135ce2..e47fed0 100644 --- a/snow/consensus/avalanche/consensus_test.go +++ b/snow/consensus/avalanche/consensus_test.go @@ -4,7 +4,9 @@ package avalanche import ( + "errors" "fmt" + "math" "testing" "github.com/prometheus/client_golang/prometheus" @@ -24,8 +26,102 @@ func GenerateID() ids.ID { var ( Genesis = GenerateID() offset = uint64(0) + + Tests = []func(*testing.T, Factory){ + MetricsTest, + ParamsTest, + AddTest, + VertexIssuedTest, + TxIssuedTest, + VirtuousTest, + VotingTest, + IgnoreInvalidVotingTest, + TransitiveVotingTest, + SplitVotingTest, + TransitiveRejectionTest, + IsVirtuousTest, + QuiesceTest, + OrphansTest, + ErrorOnVacuousAcceptTest, + ErrorOnTxAcceptTest, + ErrorOnVtxAcceptTest, + ErrorOnVtxRejectTest, + ErrorOnParentVtxRejectTest, + ErrorOnTransitiveVtxRejectTest, + } ) +func ConsensusTest(t *testing.T, factory Factory) { + for _, test := range Tests { + test(t, factory) + } +} + +func MetricsTest(t *testing.T, factory Factory) { + ctx := snow.DefaultContextTest() + + { + avl := factory.New() + params := Parameters{ + Parameters: snowball.Parameters{ + Namespace: fmt.Sprintf("gecko_%s", ctx.ChainID.String()), + Metrics: prometheus.NewRegistry(), + K: 2, + Alpha: 2, + BetaVirtuous: 1, + BetaRogue: 2, + }, + Parents: 2, + BatchSize: 1, + } + params.Metrics.Register(prometheus.NewGauge(prometheus.GaugeOpts{ + Namespace: params.Namespace, + Name: "vtx_processing", + })) + avl.Initialize(ctx, params, nil) + } + { + avl := factory.New() + params := Parameters{ + Parameters: snowball.Parameters{ + Namespace: fmt.Sprintf("gecko_%s", ctx.ChainID.String()), + Metrics: prometheus.NewRegistry(), + K: 2, + Alpha: 2, + BetaVirtuous: 1, + BetaRogue: 2, + }, + Parents: 2, + BatchSize: 1, + } + params.Metrics.Register(prometheus.NewGauge(prometheus.GaugeOpts{ + Namespace: params.Namespace, + Name: "vtx_accepted", + })) + avl.Initialize(ctx, params, nil) + } + { + avl := factory.New() + params := Parameters{ + Parameters: snowball.Parameters{ + Namespace: fmt.Sprintf("gecko_%s", ctx.ChainID.String()), + Metrics: prometheus.NewRegistry(), + K: 2, + Alpha: 2, + BetaVirtuous: 1, + BetaRogue: 2, + }, + Parents: 2, + BatchSize: 1, + } + params.Metrics.Register(prometheus.NewGauge(prometheus.GaugeOpts{ + Namespace: params.Namespace, + Name: "vtx_rejected", + })) + avl.Initialize(ctx, params, nil) + } +} + func ParamsTest(t *testing.T, factory Factory) { avl := factory.New() @@ -43,26 +139,6 @@ func ParamsTest(t *testing.T, factory Factory) { BatchSize: 1, } - numProcessing := prometheus.NewGauge( - prometheus.GaugeOpts{ - Namespace: params.Namespace, - Name: "vtx_processing", - }) - numAccepted := prometheus.NewCounter( - prometheus.CounterOpts{ - Namespace: params.Namespace, - Name: "vtx_accepted", - }) - numRejected := prometheus.NewCounter( - prometheus.CounterOpts{ - Namespace: params.Namespace, - Name: "vtx_rejected", - }) - - params.Metrics.Register(numProcessing) - params.Metrics.Register(numAccepted) - params.Metrics.Register(numRejected) - avl.Initialize(ctx, params, nil) if p := avl.Parameters(); p.K != params.K { @@ -120,9 +196,9 @@ func AddTest(t *testing.T, factory Factory) { status: choices.Processing, } - avl.Add(vtx0) - - if avl.Finalized() { + if err := avl.Add(vtx0); err != nil { + t.Fatal(err) + } else if avl.Finalized() { t.Fatalf("A non-empty avalanche instance is finalized") } else if !ids.UnsortedEquals([]ids.ID{vtx0.id}, avl.Preferences().List()) { t.Fatalf("Initial frontier failed to be set") @@ -139,25 +215,21 @@ func AddTest(t *testing.T, factory Factory) { status: choices.Processing, } - avl.Add(vtx1) - - if avl.Finalized() { + if err := avl.Add(vtx1); err != nil { + t.Fatal(err) + } else if avl.Finalized() { t.Fatalf("A non-empty avalanche instance is finalized") } else if !ids.UnsortedEquals([]ids.ID{vtx0.id}, avl.Preferences().List()) { t.Fatalf("Initial frontier failed to be set") - } - - avl.Add(vtx1) - - if avl.Finalized() { + } else if err := avl.Add(vtx1); err != nil { + t.Fatal(err) + } else if avl.Finalized() { t.Fatalf("A non-empty avalanche instance is finalized") } else if !ids.UnsortedEquals([]ids.ID{vtx0.id}, avl.Preferences().List()) { t.Fatalf("Initial frontier failed to be set") - } - - avl.Add(vts[0]) - - if avl.Finalized() { + } else if err := avl.Add(vts[0]); err != nil { + t.Fatal(err) + } else if avl.Finalized() { t.Fatalf("A non-empty avalanche instance is finalized") } else if !ids.UnsortedEquals([]ids.ID{vtx0.id}, avl.Preferences().List()) { t.Fatalf("Initial frontier failed to be set") @@ -209,11 +281,9 @@ func VertexIssuedTest(t *testing.T, factory Factory) { if avl.VertexIssued(vtx) { t.Fatalf("Vertex reported as issued") - } - - avl.Add(vtx) - - if !avl.VertexIssued(vtx) { + } else if err := avl.Add(vtx); err != nil { + t.Fatal(err) + } else if !avl.VertexIssued(vtx) { t.Fatalf("Vertex reported as not issued") } } @@ -266,9 +336,1178 @@ func TxIssuedTest(t *testing.T, factory Factory) { status: choices.Processing, } - avl.Add(vtx) - - if !avl.TxIssued(tx1) { + if err := avl.Add(vtx); err != nil { + t.Fatal(err) + } else if !avl.TxIssued(tx1) { t.Fatalf("Tx reported as not issued") } } + +func VirtuousTest(t *testing.T, factory Factory) { + avl := factory.New() + + params := Parameters{ + Parameters: snowball.Parameters{ + Metrics: prometheus.NewRegistry(), + K: 2, + Alpha: 2, + BetaVirtuous: 1, + BetaRogue: 2, + ConcurrentRepolls: 1, + }, + Parents: 2, + BatchSize: 1, + } + vts := []Vertex{&Vtx{ + id: GenerateID(), + status: choices.Accepted, + }, &Vtx{ + id: GenerateID(), + status: choices.Accepted, + }} + utxos := []ids.ID{GenerateID(), GenerateID()} + + avl.Initialize(snow.DefaultContextTest(), params, vts) + + if virtuous := avl.Virtuous(); virtuous.Len() != 2 { + t.Fatalf("Wrong number of virtuous.") + } else if !virtuous.Contains(vts[0].ID()) { + t.Fatalf("Wrong virtuous") + } else if !virtuous.Contains(vts[1].ID()) { + t.Fatalf("Wrong virtuous") + } + + tx0 := &snowstorm.TestTx{ + Identifier: GenerateID(), + Stat: choices.Processing, + } + tx0.Ins.Add(utxos[0]) + + vtx0 := &Vtx{ + dependencies: vts, + id: GenerateID(), + txs: []snowstorm.Tx{tx0}, + height: 1, + status: choices.Processing, + } + + tx1 := &snowstorm.TestTx{ + Identifier: GenerateID(), + Stat: choices.Processing, + } + tx1.Ins.Add(utxos[0]) + + vtx1 := &Vtx{ + dependencies: vts, + id: GenerateID(), + txs: []snowstorm.Tx{tx1}, + height: 1, + status: choices.Processing, + } + + tx2 := &snowstorm.TestTx{ + Identifier: GenerateID(), + Stat: choices.Processing, + } + tx2.Ins.Add(utxos[1]) + + vtx2 := &Vtx{ + dependencies: []Vertex{vtx0}, + id: GenerateID(), + txs: []snowstorm.Tx{tx2}, + height: 2, + status: choices.Processing, + } + + if err := avl.Add(vtx0); err != nil { + t.Fatal(err) + } else if virtuous := avl.Virtuous(); virtuous.Len() != 1 { + t.Fatalf("Wrong number of virtuous.") + } else if !virtuous.Contains(vtx0.id) { + t.Fatalf("Wrong virtuous") + } else if err := avl.Add(vtx1); err != nil { + t.Fatal(err) + } else if virtuous := avl.Virtuous(); virtuous.Len() != 1 { + t.Fatalf("Wrong number of virtuous.") + } else if !virtuous.Contains(vtx0.id) { + t.Fatalf("Wrong virtuous") + } else if err := avl.RecordPoll(ids.UniqueBag{}); err != nil { + t.Fatal(err) + } else if virtuous := avl.Virtuous(); virtuous.Len() != 2 { + t.Fatalf("Wrong number of virtuous.") + } else if !virtuous.Contains(vts[0].ID()) { + t.Fatalf("Wrong virtuous") + } else if !virtuous.Contains(vts[1].ID()) { + t.Fatalf("Wrong virtuous") + } else if err := avl.Add(vtx2); err != nil { + t.Fatal(err) + } else if virtuous := avl.Virtuous(); virtuous.Len() != 2 { + t.Fatalf("Wrong number of virtuous.") + } else if !virtuous.Contains(vts[0].ID()) { + t.Fatalf("Wrong virtuous") + } else if !virtuous.Contains(vts[1].ID()) { + t.Fatalf("Wrong virtuous") + } else if err := avl.RecordPoll(ids.UniqueBag{}); err != nil { + t.Fatal(err) + } else if virtuous := avl.Virtuous(); virtuous.Len() != 2 { + t.Fatalf("Wrong number of virtuous.") + } else if !virtuous.Contains(vts[0].ID()) { + t.Fatalf("Wrong virtuous") + } else if !virtuous.Contains(vts[1].ID()) { + t.Fatalf("Wrong virtuous") + } +} + +func VotingTest(t *testing.T, factory Factory) { + avl := factory.New() + + params := Parameters{ + Parameters: snowball.Parameters{ + Metrics: prometheus.NewRegistry(), + K: 2, + Alpha: 2, + BetaVirtuous: 1, + BetaRogue: 2, + ConcurrentRepolls: 1, + }, + Parents: 2, + BatchSize: 1, + } + vts := []Vertex{&Vtx{ + id: GenerateID(), + status: choices.Accepted, + }, &Vtx{ + id: GenerateID(), + status: choices.Accepted, + }} + utxos := []ids.ID{GenerateID()} + + avl.Initialize(snow.DefaultContextTest(), params, vts) + + tx0 := &snowstorm.TestTx{ + Identifier: GenerateID(), + Stat: choices.Processing, + } + tx0.Ins.Add(utxos[0]) + + vtx0 := &Vtx{ + dependencies: vts, + id: GenerateID(), + txs: []snowstorm.Tx{tx0}, + height: 1, + status: choices.Processing, + } + + tx1 := &snowstorm.TestTx{ + Identifier: GenerateID(), + Stat: choices.Processing, + } + tx1.Ins.Add(utxos[0]) + + vtx1 := &Vtx{ + dependencies: vts, + id: GenerateID(), + txs: []snowstorm.Tx{tx1}, + height: 1, + status: choices.Processing, + } + + if err := avl.Add(vtx0); err != nil { + t.Fatal(err) + } else if err := avl.Add(vtx1); err != nil { + t.Fatal(err) + } + + sm := ids.UniqueBag{} + sm.Add(0, vtx1.id) + sm.Add(1, vtx1.id) + if err := avl.RecordPoll(sm); err != nil { + t.Fatal(err) + } else if avl.Finalized() { + t.Fatalf("An avalanche instance finalized too early") + } else if !ids.UnsortedEquals([]ids.ID{vtx1.id}, avl.Preferences().List()) { + t.Fatalf("Initial frontier failed to be set") + } else if err := avl.RecordPoll(sm); err != nil { + t.Fatal(err) + } else if !avl.Finalized() { + t.Fatalf("An avalanche instance finalized too late") + } else if !ids.UnsortedEquals([]ids.ID{vtx1.id}, avl.Preferences().List()) { + t.Fatalf("Initial frontier failed to be set") + } else if tx0.Status() != choices.Rejected { + t.Fatalf("Tx should have been rejected") + } else if tx1.Status() != choices.Accepted { + t.Fatalf("Tx should have been accepted") + } +} + +func IgnoreInvalidVotingTest(t *testing.T, factory Factory) { + avl := factory.New() + + params := Parameters{ + Parameters: snowball.Parameters{ + Metrics: prometheus.NewRegistry(), + K: 3, + Alpha: 2, + BetaVirtuous: 1, + BetaRogue: 1, + }, + Parents: 2, + BatchSize: 1, + } + + vts := []Vertex{&Vtx{ + id: GenerateID(), + status: choices.Accepted, + }, &Vtx{ + id: GenerateID(), + status: choices.Accepted, + }} + utxos := []ids.ID{GenerateID()} + + avl.Initialize(snow.DefaultContextTest(), params, vts) + + tx0 := &snowstorm.TestTx{ + Identifier: GenerateID(), + Stat: choices.Processing, + } + tx0.Ins.Add(utxos[0]) + + vtx0 := &Vtx{ + dependencies: vts, + id: GenerateID(), + txs: []snowstorm.Tx{tx0}, + height: 1, + status: choices.Processing, + } + + tx1 := &snowstorm.TestTx{ + Identifier: GenerateID(), + Stat: choices.Processing, + } + tx1.Ins.Add(utxos[0]) + + vtx1 := &Vtx{ + dependencies: vts, + id: GenerateID(), + txs: []snowstorm.Tx{tx1}, + height: 1, + status: choices.Processing, + } + + if err := avl.Add(vtx0); err != nil { + t.Fatal(err) + } else if err := avl.Add(vtx1); err != nil { + t.Fatal(err) + } + + sm := ids.UniqueBag{} + sm.Add(0, vtx0.id) + sm.Add(1, vtx1.id) + + // Add Illegal Vote cast by Response 2 + sm.Add(2, vtx0.id) + sm.Add(2, vtx1.id) + + if err := avl.RecordPoll(sm); err != nil { + t.Fatal(err) + } else if avl.Finalized() { + t.Fatalf("An avalanche instance finalized too early") + } +} + +func TransitiveVotingTest(t *testing.T, factory Factory) { + avl := factory.New() + + params := Parameters{ + Parameters: snowball.Parameters{ + Metrics: prometheus.NewRegistry(), + K: 2, + Alpha: 2, + BetaVirtuous: 1, + BetaRogue: 2, + ConcurrentRepolls: 1, + }, + Parents: 2, + BatchSize: 1, + } + vts := []Vertex{&Vtx{ + id: GenerateID(), + status: choices.Accepted, + }, &Vtx{ + id: GenerateID(), + status: choices.Accepted, + }} + utxos := []ids.ID{GenerateID(), GenerateID()} + + avl.Initialize(snow.DefaultContextTest(), params, vts) + + tx0 := &snowstorm.TestTx{ + Identifier: GenerateID(), + Stat: choices.Processing, + } + tx0.Ins.Add(utxos[0]) + + vtx0 := &Vtx{ + dependencies: vts, + id: GenerateID(), + txs: []snowstorm.Tx{tx0}, + height: 1, + status: choices.Processing, + } + + tx1 := &snowstorm.TestTx{ + Identifier: GenerateID(), + Stat: choices.Processing, + } + tx1.Ins.Add(utxos[1]) + + vtx1 := &Vtx{ + dependencies: []Vertex{vtx0}, + id: GenerateID(), + txs: []snowstorm.Tx{tx1}, + height: 2, + status: choices.Processing, + } + + vtx2 := &Vtx{ + dependencies: []Vertex{vtx1}, + id: GenerateID(), + txs: []snowstorm.Tx{tx1}, + height: 3, + status: choices.Processing, + } + + if err := avl.Add(vtx0); err != nil { + t.Fatal(err) + } else if err := avl.Add(vtx1); err != nil { + t.Fatal(err) + } else if err := avl.Add(vtx2); err != nil { + t.Fatal(err) + } + + sm1 := ids.UniqueBag{} + sm1.Add(0, vtx0.id) + sm1.Add(1, vtx2.id) + if err := avl.RecordPoll(sm1); err != nil { + t.Fatal(err) + } else if avl.Finalized() { + t.Fatalf("An avalanche instance finalized too early") + } else if !ids.UnsortedEquals([]ids.ID{vtx2.id}, avl.Preferences().List()) { + t.Fatalf("Initial frontier failed to be set") + } else if tx0.Status() != choices.Accepted { + t.Fatalf("Tx should have been accepted") + } + + sm2 := ids.UniqueBag{} + sm2.Add(0, vtx2.id) + sm2.Add(1, vtx2.id) + if err := avl.RecordPoll(sm2); err != nil { + t.Fatal(err) + } else if !avl.Finalized() { + t.Fatalf("An avalanche instance finalized too late") + } else if !ids.UnsortedEquals([]ids.ID{vtx2.id}, avl.Preferences().List()) { + t.Fatalf("Initial frontier failed to be set") + } else if tx0.Status() != choices.Accepted { + t.Fatalf("Tx should have been accepted") + } else if tx1.Status() != choices.Accepted { + t.Fatalf("Tx should have been accepted") + } +} + +func SplitVotingTest(t *testing.T, factory Factory) { + avl := factory.New() + + params := Parameters{ + Parameters: snowball.Parameters{ + Metrics: prometheus.NewRegistry(), + K: 2, + Alpha: 2, + BetaVirtuous: 1, + BetaRogue: 2, + ConcurrentRepolls: 1, + }, + Parents: 2, + BatchSize: 1, + } + vts := []Vertex{&Vtx{ + id: GenerateID(), + status: choices.Accepted, + }, &Vtx{ + id: GenerateID(), + status: choices.Accepted, + }} + utxos := []ids.ID{GenerateID()} + + avl.Initialize(snow.DefaultContextTest(), params, vts) + + tx0 := &snowstorm.TestTx{ + Identifier: GenerateID(), + Stat: choices.Processing, + } + tx0.Ins.Add(utxos[0]) + + vtx0 := &Vtx{ + dependencies: vts, + id: GenerateID(), + txs: []snowstorm.Tx{tx0}, + height: 1, + status: choices.Processing, + } + + vtx1 := &Vtx{ + dependencies: vts, + id: GenerateID(), + txs: []snowstorm.Tx{tx0}, + height: 1, + status: choices.Processing, + } + + if err := avl.Add(vtx0); err != nil { + t.Fatal(err) + } else if err := avl.Add(vtx1); err != nil { + t.Fatal(err) + } + + sm1 := ids.UniqueBag{} + sm1.Add(0, vtx0.id) // peer 0 votes for the tx though vtx0 + sm1.Add(1, vtx1.id) // peer 1 votes for the tx though vtx1 + if err := avl.RecordPoll(sm1); err != nil { + t.Fatal(err) + } else if !avl.Finalized() { + t.Fatalf("An avalanche instance finalized too late") + } else if !ids.UnsortedEquals([]ids.ID{vtx0.id, vtx1.id}, avl.Preferences().List()) { + t.Fatalf("Initial frontier failed to be set") + } else if tx0.Status() != choices.Accepted { + t.Fatalf("Tx should have been accepted") + } +} + +func TransitiveRejectionTest(t *testing.T, factory Factory) { + avl := factory.New() + + params := Parameters{ + Parameters: snowball.Parameters{ + Metrics: prometheus.NewRegistry(), + K: 2, + Alpha: 2, + BetaVirtuous: 1, + BetaRogue: 2, + ConcurrentRepolls: 1, + }, + Parents: 2, + BatchSize: 1, + } + vts := []Vertex{&Vtx{ + id: GenerateID(), + status: choices.Accepted, + }, &Vtx{ + id: GenerateID(), + status: choices.Accepted, + }} + utxos := []ids.ID{GenerateID(), GenerateID()} + + avl.Initialize(snow.DefaultContextTest(), params, vts) + + tx0 := &snowstorm.TestTx{ + Identifier: GenerateID(), + Stat: choices.Processing, + } + tx0.Ins.Add(utxos[0]) + + vtx0 := &Vtx{ + dependencies: vts, + id: GenerateID(), + txs: []snowstorm.Tx{tx0}, + height: 1, + status: choices.Processing, + } + + tx1 := &snowstorm.TestTx{ + Identifier: GenerateID(), + Stat: choices.Processing, + } + tx1.Ins.Add(utxos[0]) + + vtx1 := &Vtx{ + dependencies: vts, + id: GenerateID(), + txs: []snowstorm.Tx{tx1}, + height: 1, + status: choices.Processing, + } + + tx2 := &snowstorm.TestTx{ + Identifier: GenerateID(), + Stat: choices.Processing, + } + tx2.Ins.Add(utxos[1]) + + vtx2 := &Vtx{ + dependencies: []Vertex{vtx0}, + id: GenerateID(), + txs: []snowstorm.Tx{tx2}, + height: 2, + status: choices.Processing, + } + + if err := avl.Add(vtx0); err != nil { + t.Fatal(err) + } else if err := avl.Add(vtx1); err != nil { + t.Fatal(err) + } else if err := avl.Add(vtx2); err != nil { + t.Fatal(err) + } + + sm := ids.UniqueBag{} + sm.Add(0, vtx1.id) + sm.Add(1, vtx1.id) + if err := avl.RecordPoll(sm); err != nil { + t.Fatal(err) + } else if avl.Finalized() { + t.Fatalf("An avalanche instance finalized too early") + } else if !ids.UnsortedEquals([]ids.ID{vtx1.id}, avl.Preferences().List()) { + t.Fatalf("Initial frontier failed to be set") + } else if err := avl.RecordPoll(sm); err != nil { + t.Fatal(err) + } else if avl.Finalized() { + t.Fatalf("An avalanche instance finalized too early") + } else if !ids.UnsortedEquals([]ids.ID{vtx1.id}, avl.Preferences().List()) { + t.Fatalf("Initial frontier failed to be set") + } else if tx0.Status() != choices.Rejected { + t.Fatalf("Tx should have been rejected") + } else if tx1.Status() != choices.Accepted { + t.Fatalf("Tx should have been accepted") + } else if tx2.Status() != choices.Processing { + t.Fatalf("Tx should not have been decided") + } else if err := avl.RecordPoll(sm); err != nil { + t.Fatal(err) + } else if avl.Finalized() { + t.Fatalf("An avalanche instance finalized too early") + } else if !ids.UnsortedEquals([]ids.ID{vtx1.id}, avl.Preferences().List()) { + t.Fatalf("Initial frontier failed to be set") + } else if tx0.Status() != choices.Rejected { + t.Fatalf("Tx should have been rejected") + } else if tx1.Status() != choices.Accepted { + t.Fatalf("Tx should have been accepted") + } else if tx2.Status() != choices.Processing { + t.Fatalf("Tx should not have been decided") + } +} + +func IsVirtuousTest(t *testing.T, factory Factory) { + avl := factory.New() + + params := Parameters{ + Parameters: snowball.Parameters{ + Metrics: prometheus.NewRegistry(), + K: 2, + Alpha: 2, + BetaVirtuous: 1, + BetaRogue: 2, + ConcurrentRepolls: 1, + }, + Parents: 2, + BatchSize: 1, + } + vts := []Vertex{&Vtx{ + id: GenerateID(), + status: choices.Accepted, + }, &Vtx{ + id: GenerateID(), + status: choices.Accepted, + }} + utxos := []ids.ID{GenerateID(), GenerateID()} + + avl.Initialize(snow.DefaultContextTest(), params, vts) + + if virtuous := avl.Virtuous(); virtuous.Len() != 2 { + t.Fatalf("Wrong number of virtuous.") + } else if !virtuous.Contains(vts[0].ID()) { + t.Fatalf("Wrong virtuous") + } else if !virtuous.Contains(vts[1].ID()) { + t.Fatalf("Wrong virtuous") + } + + tx0 := &snowstorm.TestTx{ + Identifier: GenerateID(), + Stat: choices.Processing, + } + tx0.Ins.Add(utxos[0]) + + vtx0 := &Vtx{ + dependencies: vts, + id: GenerateID(), + txs: []snowstorm.Tx{tx0}, + height: 1, + status: choices.Processing, + } + + tx1 := &snowstorm.TestTx{ + Identifier: GenerateID(), + Stat: choices.Processing, + } + tx1.Ins.Add(utxos[0]) + + vtx1 := &Vtx{ + dependencies: vts, + id: GenerateID(), + txs: []snowstorm.Tx{tx1}, + height: 1, + status: choices.Processing, + } + + if !avl.IsVirtuous(tx0) { + t.Fatalf("Should be virtuous.") + } else if !avl.IsVirtuous(tx1) { + t.Fatalf("Should be virtuous.") + } else if err := avl.Add(vtx0); err != nil { + t.Fatal(err) + } else if !avl.IsVirtuous(tx0) { + t.Fatalf("Should be virtuous.") + } else if avl.IsVirtuous(tx1) { + t.Fatalf("Should not be virtuous.") + } else if err := avl.Add(vtx1); err != nil { + t.Fatal(err) + } else if avl.IsVirtuous(tx0) { + t.Fatalf("Should not be virtuous.") + } else if avl.IsVirtuous(tx1) { + t.Fatalf("Should not be virtuous.") + } +} + +func QuiesceTest(t *testing.T, factory Factory) { + avl := factory.New() + + params := Parameters{ + Parameters: snowball.Parameters{ + Metrics: prometheus.NewRegistry(), + K: 1, + Alpha: 1, + BetaVirtuous: 1, + BetaRogue: 1, + ConcurrentRepolls: 1, + }, + Parents: 2, + BatchSize: 1, + } + vts := []Vertex{&Vtx{ + id: GenerateID(), + status: choices.Accepted, + }, &Vtx{ + id: GenerateID(), + status: choices.Accepted, + }} + utxos := []ids.ID{GenerateID(), GenerateID()} + + avl.Initialize(snow.DefaultContextTest(), params, vts) + + tx0 := &snowstorm.TestTx{ + Identifier: GenerateID(), + Stat: choices.Processing, + } + tx0.Ins.Add(utxos[0]) + + vtx0 := &Vtx{ + dependencies: vts, + id: GenerateID(), + txs: []snowstorm.Tx{tx0}, + height: 1, + status: choices.Processing, + } + + tx1 := &snowstorm.TestTx{ + Identifier: GenerateID(), + Stat: choices.Processing, + } + tx1.Ins.Add(utxos[0]) + + vtx1 := &Vtx{ + dependencies: vts, + id: GenerateID(), + txs: []snowstorm.Tx{tx1}, + height: 1, + status: choices.Processing, + } + + tx2 := &snowstorm.TestTx{ + Identifier: GenerateID(), + Stat: choices.Processing, + } + tx2.Ins.Add(utxos[1]) + + vtx2 := &Vtx{ + dependencies: vts, + id: GenerateID(), + txs: []snowstorm.Tx{tx2}, + height: 2, + status: choices.Processing, + } + + if err := avl.Add(vtx0); err != nil { + t.Fatal(err) + } else if avl.Quiesce() { + t.Fatalf("Shouldn't quiesce") + } else if err := avl.Add(vtx1); err != nil { + t.Fatal(err) + } else if !avl.Quiesce() { + t.Fatalf("Should quiesce") + } else if err := avl.Add(vtx2); err != nil { + t.Fatal(err) + } else if avl.Quiesce() { + t.Fatalf("Shouldn't quiesce") + } + + sm := ids.UniqueBag{} + sm.Add(0, vtx2.id) + if err := avl.RecordPoll(sm); err != nil { + t.Fatal(err) + } else if !avl.Quiesce() { + t.Fatalf("Should quiesce") + } +} + +func OrphansTest(t *testing.T, factory Factory) { + avl := factory.New() + + params := Parameters{ + Parameters: snowball.Parameters{ + Metrics: prometheus.NewRegistry(), + K: 1, + Alpha: 1, + BetaVirtuous: math.MaxInt32, + BetaRogue: math.MaxInt32, + ConcurrentRepolls: 1, + }, + Parents: 2, + BatchSize: 1, + } + vts := []Vertex{&Vtx{ + id: GenerateID(), + status: choices.Accepted, + }, &Vtx{ + id: GenerateID(), + status: choices.Accepted, + }} + utxos := []ids.ID{GenerateID(), GenerateID()} + + avl.Initialize(snow.DefaultContextTest(), params, vts) + + tx0 := &snowstorm.TestTx{ + Identifier: GenerateID(), + Stat: choices.Processing, + } + tx0.Ins.Add(utxos[0]) + + vtx0 := &Vtx{ + dependencies: vts, + id: GenerateID(), + txs: []snowstorm.Tx{tx0}, + height: 1, + status: choices.Processing, + } + + tx1 := &snowstorm.TestTx{ + Identifier: GenerateID(), + Stat: choices.Processing, + } + tx1.Ins.Add(utxos[0]) + + vtx1 := &Vtx{ + dependencies: vts, + id: GenerateID(), + txs: []snowstorm.Tx{tx1}, + height: 1, + status: choices.Processing, + } + + tx2 := &snowstorm.TestTx{ + Identifier: GenerateID(), + Stat: choices.Processing, + } + tx2.Ins.Add(utxos[1]) + + vtx2 := &Vtx{ + dependencies: []Vertex{vtx0}, + id: GenerateID(), + txs: []snowstorm.Tx{tx2}, + height: 2, + status: choices.Processing, + } + + if err := avl.Add(vtx0); err != nil { + t.Fatal(err) + } else if orphans := avl.Orphans(); orphans.Len() != 0 { + t.Fatalf("Wrong number of orphans") + } else if err := avl.Add(vtx1); err != nil { + t.Fatal(err) + } else if orphans := avl.Orphans(); orphans.Len() != 0 { + t.Fatalf("Wrong number of orphans") + } else if err := avl.Add(vtx2); err != nil { + t.Fatal(err) + } else if orphans := avl.Orphans(); orphans.Len() != 0 { + t.Fatalf("Wrong number of orphans") + } + + sm := ids.UniqueBag{} + sm.Add(0, vtx1.id) + if err := avl.RecordPoll(sm); err != nil { + t.Fatal(err) + } else if orphans := avl.Orphans(); orphans.Len() != 1 { + t.Fatalf("Wrong number of orphans") + } else if !orphans.Contains(tx2.ID()) { + t.Fatalf("Wrong orphan") + } +} + +func ErrorOnVacuousAcceptTest(t *testing.T, factory Factory) { + avl := factory.New() + + params := Parameters{ + Parameters: snowball.Parameters{ + Metrics: prometheus.NewRegistry(), + K: 1, + Alpha: 1, + BetaVirtuous: math.MaxInt32, + BetaRogue: math.MaxInt32, + ConcurrentRepolls: 1, + }, + Parents: 2, + BatchSize: 1, + } + vts := []Vertex{&Vtx{ + id: GenerateID(), + status: choices.Accepted, + }} + + avl.Initialize(snow.DefaultContextTest(), params, vts) + + tx0 := &snowstorm.TestTx{ + Identifier: GenerateID(), + Stat: choices.Processing, + Validity: errors.New(""), + } + + vtx0 := &Vtx{ + dependencies: vts, + id: GenerateID(), + txs: []snowstorm.Tx{tx0}, + height: 1, + status: choices.Processing, + } + + if err := avl.Add(vtx0); err == nil { + t.Fatalf("Should have errored on vertex issuance") + } +} + +func ErrorOnTxAcceptTest(t *testing.T, factory Factory) { + avl := factory.New() + + params := Parameters{ + Parameters: snowball.Parameters{ + Metrics: prometheus.NewRegistry(), + K: 1, + Alpha: 1, + BetaVirtuous: 1, + BetaRogue: 1, + ConcurrentRepolls: 1, + }, + Parents: 2, + BatchSize: 1, + } + vts := []Vertex{&Vtx{ + id: GenerateID(), + status: choices.Accepted, + }} + utxos := []ids.ID{GenerateID()} + + avl.Initialize(snow.DefaultContextTest(), params, vts) + + tx0 := &snowstorm.TestTx{ + Identifier: GenerateID(), + Stat: choices.Processing, + Validity: errors.New(""), + } + tx0.Ins.Add(utxos[0]) + + vtx0 := &Vtx{ + dependencies: vts, + id: GenerateID(), + txs: []snowstorm.Tx{tx0}, + height: 1, + status: choices.Processing, + } + + if err := avl.Add(vtx0); err != nil { + t.Fatal(err) + } + + votes := ids.UniqueBag{} + votes.Add(0, vtx0.id) + if err := avl.RecordPoll(votes); err == nil { + t.Fatalf("Should have errored on vertex acceptance") + } +} + +func ErrorOnVtxAcceptTest(t *testing.T, factory Factory) { + avl := factory.New() + + params := Parameters{ + Parameters: snowball.Parameters{ + Metrics: prometheus.NewRegistry(), + K: 1, + Alpha: 1, + BetaVirtuous: 1, + BetaRogue: 1, + ConcurrentRepolls: 1, + }, + Parents: 2, + BatchSize: 1, + } + vts := []Vertex{&Vtx{ + id: GenerateID(), + status: choices.Accepted, + }} + utxos := []ids.ID{GenerateID()} + + avl.Initialize(snow.DefaultContextTest(), params, vts) + + tx0 := &snowstorm.TestTx{ + Identifier: GenerateID(), + Stat: choices.Processing, + } + tx0.Ins.Add(utxos[0]) + + vtx0 := &Vtx{ + dependencies: vts, + id: GenerateID(), + txs: []snowstorm.Tx{tx0}, + height: 1, + status: choices.Processing, + Validity: errors.New(""), + } + + if err := avl.Add(vtx0); err != nil { + t.Fatal(err) + } + + votes := ids.UniqueBag{} + votes.Add(0, vtx0.id) + if err := avl.RecordPoll(votes); err == nil { + t.Fatalf("Should have errored on vertex acceptance") + } +} + +func ErrorOnVtxRejectTest(t *testing.T, factory Factory) { + avl := factory.New() + + params := Parameters{ + Parameters: snowball.Parameters{ + Metrics: prometheus.NewRegistry(), + K: 1, + Alpha: 1, + BetaVirtuous: 1, + BetaRogue: 1, + ConcurrentRepolls: 1, + }, + Parents: 2, + BatchSize: 1, + } + vts := []Vertex{&Vtx{ + id: GenerateID(), + status: choices.Accepted, + }} + utxos := []ids.ID{GenerateID()} + + avl.Initialize(snow.DefaultContextTest(), params, vts) + + tx0 := &snowstorm.TestTx{ + Identifier: GenerateID(), + Stat: choices.Processing, + } + tx0.Ins.Add(utxos[0]) + + vtx0 := &Vtx{ + dependencies: vts, + id: GenerateID(), + txs: []snowstorm.Tx{tx0}, + height: 1, + status: choices.Processing, + } + + tx1 := &snowstorm.TestTx{ + Identifier: GenerateID(), + Stat: choices.Processing, + } + tx1.Ins.Add(utxos[0]) + + vtx1 := &Vtx{ + dependencies: vts, + id: GenerateID(), + txs: []snowstorm.Tx{tx1}, + height: 1, + status: choices.Processing, + Validity: errors.New(""), + } + + if err := avl.Add(vtx0); err != nil { + t.Fatal(err) + } else if err := avl.Add(vtx1); err != nil { + t.Fatal(err) + } + + votes := ids.UniqueBag{} + votes.Add(0, vtx0.id) + if err := avl.RecordPoll(votes); err == nil { + t.Fatalf("Should have errored on vertex rejection") + } +} + +func ErrorOnParentVtxRejectTest(t *testing.T, factory Factory) { + avl := factory.New() + + params := Parameters{ + Parameters: snowball.Parameters{ + Metrics: prometheus.NewRegistry(), + K: 1, + Alpha: 1, + BetaVirtuous: 1, + BetaRogue: 1, + ConcurrentRepolls: 1, + }, + Parents: 2, + BatchSize: 1, + } + vts := []Vertex{&Vtx{ + id: GenerateID(), + status: choices.Accepted, + }} + utxos := []ids.ID{GenerateID()} + + avl.Initialize(snow.DefaultContextTest(), params, vts) + + tx0 := &snowstorm.TestTx{ + Identifier: GenerateID(), + Stat: choices.Processing, + } + tx0.Ins.Add(utxos[0]) + + vtx0 := &Vtx{ + dependencies: vts, + id: GenerateID(), + txs: []snowstorm.Tx{tx0}, + height: 1, + status: choices.Processing, + } + + tx1 := &snowstorm.TestTx{ + Identifier: GenerateID(), + Stat: choices.Processing, + } + tx1.Ins.Add(utxos[0]) + + vtx1 := &Vtx{ + dependencies: vts, + id: GenerateID(), + txs: []snowstorm.Tx{tx1}, + height: 1, + status: choices.Processing, + Validity: errors.New(""), + } + + vtx2 := &Vtx{ + dependencies: []Vertex{vtx1}, + id: GenerateID(), + txs: []snowstorm.Tx{tx1}, + height: 1, + status: choices.Processing, + } + + if err := avl.Add(vtx0); err != nil { + t.Fatal(err) + } else if err := avl.Add(vtx1); err != nil { + t.Fatal(err) + } else if err := avl.Add(vtx2); err != nil { + t.Fatal(err) + } + + votes := ids.UniqueBag{} + votes.Add(0, vtx0.id) + if err := avl.RecordPoll(votes); err == nil { + t.Fatalf("Should have errored on vertex rejection") + } +} + +func ErrorOnTransitiveVtxRejectTest(t *testing.T, factory Factory) { + avl := factory.New() + + params := Parameters{ + Parameters: snowball.Parameters{ + Metrics: prometheus.NewRegistry(), + K: 1, + Alpha: 1, + BetaVirtuous: 1, + BetaRogue: 1, + ConcurrentRepolls: 1, + }, + Parents: 2, + BatchSize: 1, + } + vts := []Vertex{&Vtx{ + id: GenerateID(), + status: choices.Accepted, + }} + utxos := []ids.ID{GenerateID()} + + avl.Initialize(snow.DefaultContextTest(), params, vts) + + tx0 := &snowstorm.TestTx{ + Identifier: GenerateID(), + Stat: choices.Processing, + } + tx0.Ins.Add(utxos[0]) + + vtx0 := &Vtx{ + dependencies: vts, + id: GenerateID(), + txs: []snowstorm.Tx{tx0}, + height: 1, + status: choices.Processing, + } + + tx1 := &snowstorm.TestTx{ + Identifier: GenerateID(), + Stat: choices.Processing, + } + tx1.Ins.Add(utxos[0]) + + vtx1 := &Vtx{ + dependencies: vts, + id: GenerateID(), + txs: []snowstorm.Tx{tx1}, + height: 1, + status: choices.Processing, + } + + vtx2 := &Vtx{ + dependencies: []Vertex{vtx1}, + id: GenerateID(), + height: 1, + status: choices.Processing, + Validity: errors.New(""), + } + + if err := avl.Add(vtx0); err != nil { + t.Fatal(err) + } else if err := avl.Add(vtx1); err != nil { + t.Fatal(err) + } else if err := avl.Add(vtx2); err != nil { + t.Fatal(err) + } + + votes := ids.UniqueBag{} + votes.Add(0, vtx0.id) + if err := avl.RecordPoll(votes); err == nil { + t.Fatalf("Should have errored on vertex rejection") + } +} diff --git a/snow/consensus/avalanche/topological.go b/snow/consensus/avalanche/topological.go index 3fa7552..b8d128e 100644 --- a/snow/consensus/avalanche/topological.go +++ b/snow/consensus/avalanche/topological.go @@ -10,6 +10,10 @@ import ( "github.com/ava-labs/gecko/snow/consensus/snowstorm" ) +const ( + minMapSize = 16 +) + // TopologicalFactory implements Factory by returning a topological struct type TopologicalFactory struct{} @@ -65,12 +69,12 @@ func (ta *Topological) Initialize(ctx *snow.Context, params Parameters, frontier ta.ctx.Log.Error("%s", err) } - ta.nodes = make(map[[32]byte]Vertex) + ta.nodes = make(map[[32]byte]Vertex, minMapSize) ta.cg = &snowstorm.Directed{} ta.cg.Initialize(ctx, params.Parameters) - ta.frontier = make(map[[32]byte]Vertex) + ta.frontier = make(map[[32]byte]Vertex, minMapSize) for _, vtx := range frontier { ta.frontier[vtx.ID().Key()] = vtx } @@ -141,7 +145,9 @@ func (ta *Topological) RecordPoll(responses ids.UniqueBag) error { votes := ta.pushVotes(kahns, leaves) // Update the conflict graph: O(|Transactions|) ta.ctx.Log.Verbo("Updating consumer confidences based on:\n%s", &votes) - ta.cg.RecordPoll(votes) + if err := ta.cg.RecordPoll(votes); err != nil { + return err + } // Update the dag: O(|Live Set|) return ta.updateFrontiers() } @@ -157,7 +163,7 @@ func (ta *Topological) Finalized() bool { return ta.cg.Finalized() } // the non-transitively applied votes. Also returns the list of leaf nodes. func (ta *Topological) calculateInDegree( responses ids.UniqueBag) (map[[32]byte]kahnNode, []ids.ID) { - kahns := make(map[[32]byte]kahnNode) + kahns := make(map[[32]byte]kahnNode, minMapSize) leaves := ids.Set{} for _, vote := range responses.List() { @@ -231,6 +237,7 @@ func (ta *Topological) pushVotes( kahnNodes map[[32]byte]kahnNode, leaves []ids.ID) ids.Bag { votes := make(ids.UniqueBag) + txConflicts := make(map[[32]byte]ids.Set, minMapSize) for len(leaves) > 0 { newLeavesSize := len(leaves) - 1 @@ -245,6 +252,12 @@ func (ta *Topological) pushVotes( // Give the votes to the consumer txID := tx.ID() votes.UnionSet(txID, kahn.votes) + + // Map txID to set of Conflicts + txKey := txID.Key() + if _, exists := txConflicts[txKey]; !exists { + txConflicts[txKey] = ta.cg.Conflicts(tx) + } } for _, dep := range vtx.Parents() { @@ -265,6 +278,18 @@ func (ta *Topological) pushVotes( } } + // Create bag of votes for conflicting transactions + conflictingVotes := make(ids.UniqueBag) + for txHash, conflicts := range txConflicts { + txID := ids.NewID(txHash) + for conflictTxHash := range conflicts { + conflictTxID := ids.NewID(conflictTxHash) + conflictingVotes.UnionSet(txID, votes.GetSet(conflictTxID)) + } + } + + votes.Difference(&conflictingVotes) + return votes.Bag(ta.params.Alpha) } @@ -422,9 +447,9 @@ func (ta *Topological) updateFrontiers() error { ta.preferred.Clear() ta.virtuous.Clear() ta.orphans.Clear() - ta.frontier = make(map[[32]byte]Vertex) - ta.preferenceCache = make(map[[32]byte]bool) - ta.virtuousCache = make(map[[32]byte]bool) + ta.frontier = make(map[[32]byte]Vertex, minMapSize) + ta.preferenceCache = make(map[[32]byte]bool, minMapSize) + ta.virtuousCache = make(map[[32]byte]bool, minMapSize) ta.orphans.Union(ta.cg.Virtuous()) // Initially, nothing is preferred diff --git a/snow/consensus/avalanche/topological_test.go b/snow/consensus/avalanche/topological_test.go index 05046bc..4b2c617 100644 --- a/snow/consensus/avalanche/topological_test.go +++ b/snow/consensus/avalanche/topological_test.go @@ -4,758 +4,7 @@ package avalanche import ( - "math" "testing" - - "github.com/prometheus/client_golang/prometheus" - - "github.com/ava-labs/gecko/ids" - "github.com/ava-labs/gecko/snow" - "github.com/ava-labs/gecko/snow/choices" - "github.com/ava-labs/gecko/snow/consensus/snowball" - "github.com/ava-labs/gecko/snow/consensus/snowstorm" ) -func TestTopologicalParams(t *testing.T) { ParamsTest(t, TopologicalFactory{}) } - -func TestTopologicalAdd(t *testing.T) { AddTest(t, TopologicalFactory{}) } - -func TestTopologicalVertexIssued(t *testing.T) { VertexIssuedTest(t, TopologicalFactory{}) } - -func TestTopologicalTxIssued(t *testing.T) { TxIssuedTest(t, TopologicalFactory{}) } - -func TestAvalancheVoting(t *testing.T) { - params := Parameters{ - Parameters: snowball.Parameters{ - Metrics: prometheus.NewRegistry(), - K: 2, - Alpha: 2, - BetaVirtuous: 1, - BetaRogue: 2, - ConcurrentRepolls: 1, - }, - Parents: 2, - BatchSize: 1, - } - vts := []Vertex{&Vtx{ - id: GenerateID(), - status: choices.Accepted, - }, &Vtx{ - id: GenerateID(), - status: choices.Accepted, - }} - utxos := []ids.ID{GenerateID()} - - ta := Topological{} - ta.Initialize(snow.DefaultContextTest(), params, vts) - - tx0 := &snowstorm.TestTx{ - Identifier: GenerateID(), - Stat: choices.Processing, - } - tx0.Ins.Add(utxos[0]) - - vtx0 := &Vtx{ - dependencies: vts, - id: GenerateID(), - txs: []snowstorm.Tx{tx0}, - height: 1, - status: choices.Processing, - } - - tx1 := &snowstorm.TestTx{ - Identifier: GenerateID(), - Stat: choices.Processing, - } - tx1.Ins.Add(utxos[0]) - - vtx1 := &Vtx{ - dependencies: vts, - id: GenerateID(), - txs: []snowstorm.Tx{tx1}, - height: 1, - status: choices.Processing, - } - - ta.Add(vtx0) - ta.Add(vtx1) - - sm := make(ids.UniqueBag) - sm.Add(0, vtx1.id) - sm.Add(1, vtx1.id) - ta.RecordPoll(sm) - - if ta.Finalized() { - t.Fatalf("An avalanche instance finalized too early") - } else if !ids.UnsortedEquals([]ids.ID{vtx1.id}, ta.Preferences().List()) { - t.Fatalf("Initial frontier failed to be set") - } - - ta.RecordPoll(sm) - - if !ta.Finalized() { - t.Fatalf("An avalanche instance finalized too late") - } else if !ids.UnsortedEquals([]ids.ID{vtx1.id}, ta.Preferences().List()) { - t.Fatalf("Initial frontier failed to be set") - } else if tx0.Status() != choices.Rejected { - t.Fatalf("Tx should have been rejected") - } else if tx1.Status() != choices.Accepted { - t.Fatalf("Tx should have been accepted") - } -} - -func TestAvalancheTransitiveVoting(t *testing.T) { - params := Parameters{ - Parameters: snowball.Parameters{ - Metrics: prometheus.NewRegistry(), - K: 2, - Alpha: 2, - BetaVirtuous: 1, - BetaRogue: 2, - ConcurrentRepolls: 1, - }, - Parents: 2, - BatchSize: 1, - } - vts := []Vertex{&Vtx{ - id: GenerateID(), - status: choices.Accepted, - }, &Vtx{ - id: GenerateID(), - status: choices.Accepted, - }} - utxos := []ids.ID{GenerateID(), GenerateID()} - - ta := Topological{} - ta.Initialize(snow.DefaultContextTest(), params, vts) - - tx0 := &snowstorm.TestTx{ - Identifier: GenerateID(), - Stat: choices.Processing, - } - tx0.Ins.Add(utxos[0]) - - vtx0 := &Vtx{ - dependencies: vts, - id: GenerateID(), - txs: []snowstorm.Tx{tx0}, - height: 1, - status: choices.Processing, - } - - tx1 := &snowstorm.TestTx{ - Identifier: GenerateID(), - Stat: choices.Processing, - } - tx1.Ins.Add(utxos[1]) - - vtx1 := &Vtx{ - dependencies: []Vertex{vtx0}, - id: GenerateID(), - txs: []snowstorm.Tx{tx1}, - height: 2, - status: choices.Processing, - } - - vtx2 := &Vtx{ - dependencies: []Vertex{vtx1}, - id: GenerateID(), - txs: []snowstorm.Tx{tx1}, - height: 3, - status: choices.Processing, - } - - ta.Add(vtx0) - ta.Add(vtx1) - ta.Add(vtx2) - - sm1 := make(ids.UniqueBag) - sm1.Add(0, vtx0.id) - sm1.Add(1, vtx2.id) - ta.RecordPoll(sm1) - - if ta.Finalized() { - t.Fatalf("An avalanche instance finalized too early") - } else if !ids.UnsortedEquals([]ids.ID{vtx2.id}, ta.Preferences().List()) { - t.Fatalf("Initial frontier failed to be set") - } else if tx0.Status() != choices.Accepted { - t.Fatalf("Tx should have been accepted") - } - - sm2 := make(ids.UniqueBag) - sm2.Add(0, vtx2.id) - sm2.Add(1, vtx2.id) - ta.RecordPoll(sm2) - - if !ta.Finalized() { - t.Fatalf("An avalanche instance finalized too late") - } else if !ids.UnsortedEquals([]ids.ID{vtx2.id}, ta.Preferences().List()) { - t.Fatalf("Initial frontier failed to be set") - } else if tx0.Status() != choices.Accepted { - t.Fatalf("Tx should have been accepted") - } else if tx1.Status() != choices.Accepted { - t.Fatalf("Tx should have been accepted") - } -} - -func TestAvalancheSplitVoting(t *testing.T) { - params := Parameters{ - Parameters: snowball.Parameters{ - Metrics: prometheus.NewRegistry(), - K: 2, - Alpha: 2, - BetaVirtuous: 1, - BetaRogue: 2, - ConcurrentRepolls: 1, - }, - Parents: 2, - BatchSize: 1, - } - vts := []Vertex{&Vtx{ - id: GenerateID(), - status: choices.Accepted, - }, &Vtx{ - id: GenerateID(), - status: choices.Accepted, - }} - utxos := []ids.ID{GenerateID()} - - ta := Topological{} - ta.Initialize(snow.DefaultContextTest(), params, vts) - - tx0 := &snowstorm.TestTx{ - Identifier: GenerateID(), - Stat: choices.Processing, - } - tx0.Ins.Add(utxos[0]) - - vtx0 := &Vtx{ - dependencies: vts, - id: GenerateID(), - txs: []snowstorm.Tx{tx0}, - height: 1, - status: choices.Processing, - } - - vtx1 := &Vtx{ - dependencies: vts, - id: GenerateID(), - txs: []snowstorm.Tx{tx0}, - height: 1, - status: choices.Processing, - } - - ta.Add(vtx0) - ta.Add(vtx1) - - sm1 := make(ids.UniqueBag) - sm1.Add(0, vtx0.id) - sm1.Add(1, vtx1.id) - ta.RecordPoll(sm1) - - if !ta.Finalized() { - t.Fatalf("An avalanche instance finalized too late") - } else if !ids.UnsortedEquals([]ids.ID{vtx0.id, vtx1.id}, ta.Preferences().List()) { - t.Fatalf("Initial frontier failed to be set") - } else if tx0.Status() != choices.Accepted { - t.Fatalf("Tx should have been accepted") - } -} - -func TestAvalancheTransitiveRejection(t *testing.T) { - params := Parameters{ - Parameters: snowball.Parameters{ - Metrics: prometheus.NewRegistry(), - K: 2, - Alpha: 2, - BetaVirtuous: 1, - BetaRogue: 2, - ConcurrentRepolls: 1, - }, - Parents: 2, - BatchSize: 1, - } - vts := []Vertex{&Vtx{ - id: GenerateID(), - status: choices.Accepted, - }, &Vtx{ - id: GenerateID(), - status: choices.Accepted, - }} - utxos := []ids.ID{GenerateID(), GenerateID()} - - ta := Topological{} - ta.Initialize(snow.DefaultContextTest(), params, vts) - - tx0 := &snowstorm.TestTx{ - Identifier: GenerateID(), - Stat: choices.Processing, - } - tx0.Ins.Add(utxos[0]) - - vtx0 := &Vtx{ - dependencies: vts, - id: GenerateID(), - txs: []snowstorm.Tx{tx0}, - height: 1, - status: choices.Processing, - } - - tx1 := &snowstorm.TestTx{ - Identifier: GenerateID(), - Stat: choices.Processing, - } - tx1.Ins.Add(utxos[0]) - - vtx1 := &Vtx{ - dependencies: vts, - id: GenerateID(), - txs: []snowstorm.Tx{tx1}, - height: 1, - status: choices.Processing, - } - - tx2 := &snowstorm.TestTx{ - Identifier: GenerateID(), - Stat: choices.Processing, - } - tx2.Ins.Add(utxos[1]) - - vtx2 := &Vtx{ - dependencies: []Vertex{vtx0}, - id: GenerateID(), - txs: []snowstorm.Tx{tx2}, - height: 2, - status: choices.Processing, - } - - ta.Add(vtx0) - ta.Add(vtx1) - ta.Add(vtx2) - - sm := make(ids.UniqueBag) - sm.Add(0, vtx1.id) - sm.Add(1, vtx1.id) - ta.RecordPoll(sm) - - if ta.Finalized() { - t.Fatalf("An avalanche instance finalized too early") - } else if !ids.UnsortedEquals([]ids.ID{vtx1.id}, ta.Preferences().List()) { - t.Fatalf("Initial frontier failed to be set") - } - - ta.RecordPoll(sm) - - if ta.Finalized() { - t.Fatalf("An avalanche instance finalized too early") - } else if !ids.UnsortedEquals([]ids.ID{vtx1.id}, ta.Preferences().List()) { - t.Fatalf("Initial frontier failed to be set") - } else if tx0.Status() != choices.Rejected { - t.Fatalf("Tx should have been rejected") - } else if tx1.Status() != choices.Accepted { - t.Fatalf("Tx should have been accepted") - } else if tx2.Status() != choices.Processing { - t.Fatalf("Tx should not have been decided") - } - - ta.preferenceCache = make(map[[32]byte]bool) - ta.virtuousCache = make(map[[32]byte]bool) - - ta.update(vtx2) -} - -func TestAvalancheVirtuous(t *testing.T) { - params := Parameters{ - Parameters: snowball.Parameters{ - Metrics: prometheus.NewRegistry(), - K: 2, - Alpha: 2, - BetaVirtuous: 1, - BetaRogue: 2, - ConcurrentRepolls: 1, - }, - Parents: 2, - BatchSize: 1, - } - vts := []Vertex{&Vtx{ - id: GenerateID(), - status: choices.Accepted, - }, &Vtx{ - id: GenerateID(), - status: choices.Accepted, - }} - utxos := []ids.ID{GenerateID(), GenerateID()} - - ta := Topological{} - ta.Initialize(snow.DefaultContextTest(), params, vts) - - if virtuous := ta.Virtuous(); virtuous.Len() != 2 { - t.Fatalf("Wrong number of virtuous.") - } else if !virtuous.Contains(vts[0].ID()) { - t.Fatalf("Wrong virtuous") - } else if !virtuous.Contains(vts[1].ID()) { - t.Fatalf("Wrong virtuous") - } - - tx0 := &snowstorm.TestTx{ - Identifier: GenerateID(), - Stat: choices.Processing, - } - tx0.Ins.Add(utxos[0]) - - vtx0 := &Vtx{ - dependencies: vts, - id: GenerateID(), - txs: []snowstorm.Tx{tx0}, - height: 1, - status: choices.Processing, - } - - tx1 := &snowstorm.TestTx{ - Identifier: GenerateID(), - Stat: choices.Processing, - } - tx1.Ins.Add(utxos[0]) - - vtx1 := &Vtx{ - dependencies: vts, - id: GenerateID(), - txs: []snowstorm.Tx{tx1}, - height: 1, - status: choices.Processing, - } - - tx2 := &snowstorm.TestTx{ - Identifier: GenerateID(), - Stat: choices.Processing, - } - tx2.Ins.Add(utxos[1]) - - vtx2 := &Vtx{ - dependencies: []Vertex{vtx0}, - id: GenerateID(), - txs: []snowstorm.Tx{tx2}, - height: 2, - status: choices.Processing, - } - - ta.Add(vtx0) - - if virtuous := ta.Virtuous(); virtuous.Len() != 1 { - t.Fatalf("Wrong number of virtuous.") - } else if !virtuous.Contains(vtx0.id) { - t.Fatalf("Wrong virtuous") - } - - ta.Add(vtx1) - - if virtuous := ta.Virtuous(); virtuous.Len() != 1 { - t.Fatalf("Wrong number of virtuous.") - } else if !virtuous.Contains(vtx0.id) { - t.Fatalf("Wrong virtuous") - } - - ta.updateFrontiers() - - if virtuous := ta.Virtuous(); virtuous.Len() != 2 { - t.Fatalf("Wrong number of virtuous.") - } else if !virtuous.Contains(vts[0].ID()) { - t.Fatalf("Wrong virtuous") - } else if !virtuous.Contains(vts[1].ID()) { - t.Fatalf("Wrong virtuous") - } - - ta.Add(vtx2) - - if virtuous := ta.Virtuous(); virtuous.Len() != 2 { - t.Fatalf("Wrong number of virtuous.") - } else if !virtuous.Contains(vts[0].ID()) { - t.Fatalf("Wrong virtuous") - } else if !virtuous.Contains(vts[1].ID()) { - t.Fatalf("Wrong virtuous") - } - - ta.updateFrontiers() - - if virtuous := ta.Virtuous(); virtuous.Len() != 2 { - t.Fatalf("Wrong number of virtuous.") - } else if !virtuous.Contains(vts[0].ID()) { - t.Fatalf("Wrong virtuous") - } else if !virtuous.Contains(vts[1].ID()) { - t.Fatalf("Wrong virtuous") - } -} - -func TestAvalancheIsVirtuous(t *testing.T) { - params := Parameters{ - Parameters: snowball.Parameters{ - Metrics: prometheus.NewRegistry(), - K: 2, - Alpha: 2, - BetaVirtuous: 1, - BetaRogue: 2, - ConcurrentRepolls: 1, - }, - Parents: 2, - BatchSize: 1, - } - vts := []Vertex{&Vtx{ - id: GenerateID(), - status: choices.Accepted, - }, &Vtx{ - id: GenerateID(), - status: choices.Accepted, - }} - utxos := []ids.ID{GenerateID(), GenerateID()} - - ta := Topological{} - ta.Initialize(snow.DefaultContextTest(), params, vts) - - if virtuous := ta.Virtuous(); virtuous.Len() != 2 { - t.Fatalf("Wrong number of virtuous.") - } else if !virtuous.Contains(vts[0].ID()) { - t.Fatalf("Wrong virtuous") - } else if !virtuous.Contains(vts[1].ID()) { - t.Fatalf("Wrong virtuous") - } - - tx0 := &snowstorm.TestTx{ - Identifier: GenerateID(), - Stat: choices.Processing, - } - tx0.Ins.Add(utxos[0]) - - vtx0 := &Vtx{ - dependencies: vts, - id: GenerateID(), - txs: []snowstorm.Tx{tx0}, - height: 1, - status: choices.Processing, - } - - tx1 := &snowstorm.TestTx{ - Identifier: GenerateID(), - Stat: choices.Processing, - } - tx1.Ins.Add(utxos[0]) - - vtx1 := &Vtx{ - dependencies: vts, - id: GenerateID(), - txs: []snowstorm.Tx{tx1}, - height: 1, - status: choices.Processing, - } - - if !ta.IsVirtuous(tx0) { - t.Fatalf("Should be virtuous.") - } else if !ta.IsVirtuous(tx1) { - t.Fatalf("Should be virtuous.") - } - - ta.Add(vtx0) - - if !ta.IsVirtuous(tx0) { - t.Fatalf("Should be virtuous.") - } else if ta.IsVirtuous(tx1) { - t.Fatalf("Should not be virtuous.") - } - - ta.Add(vtx1) - - if ta.IsVirtuous(tx0) { - t.Fatalf("Should not be virtuous.") - } else if ta.IsVirtuous(tx1) { - t.Fatalf("Should not be virtuous.") - } -} - -func TestAvalancheQuiesce(t *testing.T) { - params := Parameters{ - Parameters: snowball.Parameters{ - Metrics: prometheus.NewRegistry(), - K: 1, - Alpha: 1, - BetaVirtuous: 1, - BetaRogue: 1, - ConcurrentRepolls: 1, - }, - Parents: 2, - BatchSize: 1, - } - vts := []Vertex{&Vtx{ - id: GenerateID(), - status: choices.Accepted, - }, &Vtx{ - id: GenerateID(), - status: choices.Accepted, - }} - utxos := []ids.ID{GenerateID(), GenerateID()} - - ta := Topological{} - ta.Initialize(snow.DefaultContextTest(), params, vts) - - tx0 := &snowstorm.TestTx{ - Identifier: GenerateID(), - Stat: choices.Processing, - } - tx0.Ins.Add(utxos[0]) - - vtx0 := &Vtx{ - dependencies: vts, - id: GenerateID(), - txs: []snowstorm.Tx{tx0}, - height: 1, - status: choices.Processing, - } - - tx1 := &snowstorm.TestTx{ - Identifier: GenerateID(), - Stat: choices.Processing, - } - tx1.Ins.Add(utxos[0]) - - vtx1 := &Vtx{ - dependencies: vts, - id: GenerateID(), - txs: []snowstorm.Tx{tx1}, - height: 1, - status: choices.Processing, - } - - tx2 := &snowstorm.TestTx{ - Identifier: GenerateID(), - Stat: choices.Processing, - } - tx2.Ins.Add(utxos[1]) - - vtx2 := &Vtx{ - dependencies: vts, - id: GenerateID(), - txs: []snowstorm.Tx{tx2}, - height: 2, - status: choices.Processing, - } - - ta.Add(vtx0) - - if ta.Quiesce() { - t.Fatalf("Shouldn't quiesce") - } - - ta.Add(vtx1) - - if !ta.Quiesce() { - t.Fatalf("Should quiesce") - } - - ta.Add(vtx2) - - if ta.Quiesce() { - t.Fatalf("Shouldn't quiesce") - } - - sm := make(ids.UniqueBag) - sm.Add(0, vtx2.id) - ta.RecordPoll(sm) - - if !ta.Quiesce() { - t.Fatalf("Should quiesce") - } -} - -func TestAvalancheOrphans(t *testing.T) { - params := Parameters{ - Parameters: snowball.Parameters{ - Metrics: prometheus.NewRegistry(), - K: 1, - Alpha: 1, - BetaVirtuous: math.MaxInt32, - BetaRogue: math.MaxInt32, - ConcurrentRepolls: 1, - }, - Parents: 2, - BatchSize: 1, - } - vts := []Vertex{&Vtx{ - id: GenerateID(), - status: choices.Accepted, - }, &Vtx{ - id: GenerateID(), - status: choices.Accepted, - }} - utxos := []ids.ID{GenerateID(), GenerateID()} - - ta := Topological{} - ta.Initialize(snow.DefaultContextTest(), params, vts) - - tx0 := &snowstorm.TestTx{ - Identifier: GenerateID(), - Stat: choices.Processing, - } - tx0.Ins.Add(utxos[0]) - - vtx0 := &Vtx{ - dependencies: vts, - id: GenerateID(), - txs: []snowstorm.Tx{tx0}, - height: 1, - status: choices.Processing, - } - - tx1 := &snowstorm.TestTx{ - Identifier: GenerateID(), - Stat: choices.Processing, - } - tx1.Ins.Add(utxos[0]) - - vtx1 := &Vtx{ - dependencies: vts, - id: GenerateID(), - txs: []snowstorm.Tx{tx1}, - height: 1, - status: choices.Processing, - } - - tx2 := &snowstorm.TestTx{ - Identifier: GenerateID(), - Stat: choices.Processing, - } - tx2.Ins.Add(utxos[1]) - - vtx2 := &Vtx{ - dependencies: []Vertex{vtx0}, - id: GenerateID(), - txs: []snowstorm.Tx{tx2}, - height: 2, - status: choices.Processing, - } - - ta.Add(vtx0) - - if orphans := ta.Orphans(); orphans.Len() != 0 { - t.Fatalf("Wrong number of orphans") - } - - ta.Add(vtx1) - - if orphans := ta.Orphans(); orphans.Len() != 0 { - t.Fatalf("Wrong number of orphans") - } - - ta.Add(vtx2) - - if orphans := ta.Orphans(); orphans.Len() != 0 { - t.Fatalf("Wrong number of orphans") - } - - sm := make(ids.UniqueBag) - sm.Add(0, vtx1.id) - ta.RecordPoll(sm) - - if orphans := ta.Orphans(); orphans.Len() != 1 { - t.Fatalf("Wrong number of orphans") - } else if !orphans.Contains(tx2.ID()) { - t.Fatalf("Wrong orphan") - } -} +func TestTopological(t *testing.T) { ConsensusTest(t, TopologicalFactory{}) } diff --git a/snow/consensus/avalanche/vertex_test.go b/snow/consensus/avalanche/vertex_test.go index f7aee5a..0270af5 100644 --- a/snow/consensus/avalanche/vertex_test.go +++ b/snow/consensus/avalanche/vertex_test.go @@ -19,7 +19,8 @@ type Vtx struct { height uint64 status choices.Status - bytes []byte + Validity error + bytes []byte } func (v *Vtx) ID() ids.ID { return v.id } @@ -28,9 +29,8 @@ func (v *Vtx) Parents() []Vertex { return v.dependencies } func (v *Vtx) Height() uint64 { return v.height } func (v *Vtx) Txs() []snowstorm.Tx { return v.txs } func (v *Vtx) Status() choices.Status { return v.status } -func (v *Vtx) Live() {} -func (v *Vtx) Accept() error { v.status = choices.Accepted; return nil } -func (v *Vtx) Reject() error { v.status = choices.Rejected; return nil } +func (v *Vtx) Accept() error { v.status = choices.Accepted; return v.Validity } +func (v *Vtx) Reject() error { v.status = choices.Rejected; return v.Validity } func (v *Vtx) Bytes() []byte { return v.bytes } type sortVts []*Vtx diff --git a/snow/consensus/snowball/byzantine.go b/snow/consensus/snowball/byzantine.go deleted file mode 100644 index 88fda59..0000000 --- a/snow/consensus/snowball/byzantine.go +++ /dev/null @@ -1,48 +0,0 @@ -// (c) 2019-2020, Ava Labs, Inc. All rights reserved. -// See the file LICENSE for licensing terms. - -package snowball - -import ( - "github.com/ava-labs/gecko/ids" -) - -// ByzantineFactory implements Factory by returning a byzantine struct -type ByzantineFactory struct{} - -// New implements Factory -func (ByzantineFactory) New() Consensus { return &Byzantine{} } - -// Byzantine is a naive implementation of a multi-choice snowball instance -type Byzantine struct { - // params contains all the configurations of a snowball instance - params Parameters - - // Hardcode the preference - preference ids.ID -} - -// Initialize implements the Consensus interface -func (b *Byzantine) Initialize(params Parameters, choice ids.ID) { - b.params = params - b.preference = choice -} - -// Parameters implements the Consensus interface -func (b *Byzantine) Parameters() Parameters { return b.params } - -// Add implements the Consensus interface -func (b *Byzantine) Add(choice ids.ID) {} - -// Preference implements the Consensus interface -func (b *Byzantine) Preference() ids.ID { return b.preference } - -// RecordPoll implements the Consensus interface -func (b *Byzantine) RecordPoll(votes ids.Bag) {} - -// RecordUnsuccessfulPoll implements the Consensus interface -func (b *Byzantine) RecordUnsuccessfulPoll() {} - -// Finalized implements the Consensus interface -func (b *Byzantine) Finalized() bool { return true } -func (b *Byzantine) String() string { return b.preference.String() } diff --git a/snow/consensus/snowball/byzantine_test.go b/snow/consensus/snowball/byzantine_test.go deleted file mode 100644 index cee357b..0000000 --- a/snow/consensus/snowball/byzantine_test.go +++ /dev/null @@ -1,54 +0,0 @@ -// (c) 2019-2020, Ava Labs, Inc. All rights reserved. -// See the file LICENSE for licensing terms. - -package snowball - -import ( - "testing" - - "github.com/ava-labs/gecko/ids" - "github.com/prometheus/client_golang/prometheus" -) - -func TestByzantine(t *testing.T) { - params := Parameters{ - Metrics: prometheus.NewRegistry(), - K: 1, Alpha: 1, BetaVirtuous: 3, BetaRogue: 5, - } - - byzFactory := ByzantineFactory{} - byz := byzFactory.New() - byz.Initialize(params, Blue) - - if ret := byz.Parameters(); ret != params { - t.Fatalf("Should have returned the correct params") - } - - byz.Add(Green) - - if pref := byz.Preference(); !pref.Equals(Blue) { - t.Fatalf("Wrong preference, expected %s returned %s", Blue, pref) - } - - oneGreen := ids.Bag{} - oneGreen.Add(Green) - byz.RecordPoll(oneGreen) - - if pref := byz.Preference(); !pref.Equals(Blue) { - t.Fatalf("Wrong preference, expected %s returned %s", Blue, pref) - } - - byz.RecordUnsuccessfulPoll() - - if pref := byz.Preference(); !pref.Equals(Blue) { - t.Fatalf("Wrong preference, expected %s returned %s", Blue, pref) - } - - if final := byz.Finalized(); !final { - t.Fatalf("Should be marked as accepted") - } - - if str := byz.String(); str != Blue.String() { - t.Fatalf("Wrong string, expected %s returned %s", Blue, str) - } -} diff --git a/snow/consensus/snowball/consensus_test.go b/snow/consensus/snowball/consensus_test.go index 922f606..304bc19 100644 --- a/snow/consensus/snowball/consensus_test.go +++ b/snow/consensus/snowball/consensus_test.go @@ -11,6 +11,46 @@ import ( "github.com/ava-labs/gecko/ids" ) +// ByzantineFactory implements Factory by returning a byzantine struct +type ByzantineFactory struct{} + +// New implements Factory +func (ByzantineFactory) New() Consensus { return &Byzantine{} } + +// Byzantine is a naive implementation of a multi-choice snowball instance +type Byzantine struct { + // params contains all the configurations of a snowball instance + params Parameters + + // Hardcode the preference + preference ids.ID +} + +// Initialize implements the Consensus interface +func (b *Byzantine) Initialize(params Parameters, choice ids.ID) { + b.params = params + b.preference = choice +} + +// Parameters implements the Consensus interface +func (b *Byzantine) Parameters() Parameters { return b.params } + +// Add implements the Consensus interface +func (b *Byzantine) Add(choice ids.ID) {} + +// Preference implements the Consensus interface +func (b *Byzantine) Preference() ids.ID { return b.preference } + +// RecordPoll implements the Consensus interface +func (b *Byzantine) RecordPoll(votes ids.Bag) {} + +// RecordUnsuccessfulPoll implements the Consensus interface +func (b *Byzantine) RecordUnsuccessfulPoll() {} + +// Finalized implements the Consensus interface +func (b *Byzantine) Finalized() bool { return true } +func (b *Byzantine) String() string { return b.preference.String() } + var ( Red = ids.Empty.Prefix(0) Blue = ids.Empty.Prefix(1) diff --git a/snow/consensus/snowball/flat.go b/snow/consensus/snowball/flat.go index 21663c4..464e525 100644 --- a/snow/consensus/snowball/flat.go +++ b/snow/consensus/snowball/flat.go @@ -34,7 +34,7 @@ func (f *Flat) Parameters() Parameters { return f.params } // RecordPoll implements the Consensus interface func (f *Flat) RecordPoll(votes ids.Bag) { if pollMode, numVotes := votes.Mode(); numVotes >= f.params.Alpha { - f.nnarySnowball.RecordSuccessfulPoll(pollMode) + f.RecordSuccessfulPoll(pollMode) } else { f.RecordUnsuccessfulPoll() } diff --git a/snow/consensus/snowball/nnary_snowflake.go b/snow/consensus/snowball/nnary_snowflake.go index 8b461f0..ab580f9 100644 --- a/snow/consensus/snowball/nnary_snowflake.go +++ b/snow/consensus/snowball/nnary_snowflake.go @@ -51,7 +51,7 @@ func (sf *nnarySnowflake) RecordSuccessfulPoll(choice ids.ID) { return // This instace is already decided. } - if preference := sf.nnarySlush.Preference(); preference.Equals(choice) { + if preference := sf.Preference(); preference.Equals(choice) { sf.confidence++ } else { // confidence is set to 1 because there has already been 1 successful diff --git a/snow/consensus/snowball/parameters_test.go b/snow/consensus/snowball/parameters_test.go index 7c3668c..056e229 100644 --- a/snow/consensus/snowball/parameters_test.go +++ b/snow/consensus/snowball/parameters_test.go @@ -125,14 +125,14 @@ func TestParametersAnotherInvalidBetaRogue(t *testing.T) { func TestParametersInvalidConcurrentRepolls(t *testing.T) { tests := []Parameters{ - Parameters{ + { K: 1, Alpha: 1, BetaVirtuous: 1, BetaRogue: 1, ConcurrentRepolls: 2, }, - Parameters{ + { K: 1, Alpha: 1, BetaVirtuous: 1, diff --git a/snow/consensus/snowball/unary_snowball.go b/snow/consensus/snowball/unary_snowball.go index 26ea35d..44eea46 100644 --- a/snow/consensus/snowball/unary_snowball.go +++ b/snow/consensus/snowball/unary_snowball.go @@ -27,11 +27,13 @@ func (sb *unarySnowball) Extend(beta int, choice int) BinarySnowball { bs := &binarySnowball{ binarySnowflake: binarySnowflake{ binarySlush: binarySlush{preference: choice}, + confidence: sb.confidence, beta: beta, finalized: sb.Finalized(), }, preference: choice, } + bs.numSuccessfulPolls[choice] = sb.numSuccessfulPolls return bs } diff --git a/snow/consensus/snowball/unary_snowball_test.go b/snow/consensus/snowball/unary_snowball_test.go index 3f4efe5..4ddc6c6 100644 --- a/snow/consensus/snowball/unary_snowball_test.go +++ b/snow/consensus/snowball/unary_snowball_test.go @@ -42,11 +42,32 @@ func TestUnarySnowball(t *testing.T) { binarySnowball := sbClone.Extend(beta, 0) + expected := "SB(Preference = 0, NumSuccessfulPolls[0] = 2, NumSuccessfulPolls[1] = 0, SF(Confidence = 1, Finalized = false, SL(Preference = 0)))" + if result := binarySnowball.String(); result != expected { + t.Fatalf("Expected:\n%s\nReturned:\n%s", expected, result) + } + binarySnowball.RecordUnsuccessfulPoll() + for i := 0; i < 3; i++ { + if binarySnowball.Preference() != 0 { + t.Fatalf("Wrong preference") + } else if binarySnowball.Finalized() { + t.Fatalf("Should not have finalized") + } + binarySnowball.RecordSuccessfulPoll(1) + binarySnowball.RecordUnsuccessfulPoll() + } + + if binarySnowball.Preference() != 1 { + t.Fatalf("Wrong preference") + } else if binarySnowball.Finalized() { + t.Fatalf("Should not have finalized") + } binarySnowball.RecordSuccessfulPoll(1) - - if binarySnowball.Finalized() { + if binarySnowball.Preference() != 1 { + t.Fatalf("Wrong preference") + } else if binarySnowball.Finalized() { t.Fatalf("Should not have finalized") } @@ -57,4 +78,9 @@ func TestUnarySnowball(t *testing.T) { } else if !binarySnowball.Finalized() { t.Fatalf("Should have finalized") } + + expected = "SB(NumSuccessfulPolls = 2, SF(Confidence = 1, Finalized = false))" + if str := sb.String(); str != expected { + t.Fatalf("Wrong state. Expected:\n%s\nGot:\n%s", expected, str) + } } diff --git a/snow/consensus/snowman/block_test.go b/snow/consensus/snowman/block_test.go index e03e6c2..d7db116 100644 --- a/snow/consensus/snowman/block_test.go +++ b/snow/consensus/snowman/block_test.go @@ -17,6 +17,7 @@ type TestBlock struct { height int status choices.Status bytes []byte + err error } func (b *TestBlock) Parent() Block { return b.parent } @@ -27,16 +28,16 @@ func (b *TestBlock) Accept() error { return errors.New("Dis-agreement") } b.status = choices.Accepted - return nil + return b.err } func (b *TestBlock) Reject() error { if b.status.Decided() && b.status != choices.Rejected { return errors.New("Dis-agreement") } b.status = choices.Rejected - return nil + return b.err } -func (b *TestBlock) Verify() error { return nil } +func (b *TestBlock) Verify() error { return b.err } func (b *TestBlock) Bytes() []byte { return b.bytes } type sortBlocks []*TestBlock diff --git a/snow/consensus/snowman/consensus_test.go b/snow/consensus/snowman/consensus_test.go index f64b4d8..3d3fe7f 100644 --- a/snow/consensus/snowman/consensus_test.go +++ b/snow/consensus/snowman/consensus_test.go @@ -4,6 +4,7 @@ package snowman import ( + "errors" "math/rand" "testing" @@ -42,6 +43,10 @@ var ( MetricsProcessingErrorTest, MetricsAcceptedErrorTest, MetricsRejectedErrorTest, + ErrorOnInitialRejectionTest, + ErrorOnAcceptTest, + ErrorOnRejectSiblingTest, + ErrorOnTransitiveRejectionTest, RandomizedConsistencyTest, } ) @@ -71,11 +76,9 @@ func InitializeTest(t *testing.T, factory Factory) { if p := sm.Parameters(); p != params { t.Fatalf("Wrong returned parameters") - } - if pref := sm.Preference(); !pref.Equals(GenesisID) { + } else if pref := sm.Preference(); !pref.Equals(GenesisID) { t.Fatalf("Wrong preference returned") - } - if !sm.Finalized() { + } else if !sm.Finalized() { t.Fatalf("Wrong should have marked the instance as being finalized") } } @@ -101,9 +104,9 @@ func AddToTailTest(t *testing.T, factory Factory) { } // Adding to the previous preference will update the preference - sm.Add(block) - - if pref := sm.Preference(); !pref.Equals(block.id) { + if err := sm.Add(block); err != nil { + t.Fatal(err) + } else if pref := sm.Preference(); !pref.Equals(block.id) { t.Fatalf("Wrong preference. Expected %s, got %s", block.id, pref) } } @@ -133,17 +136,17 @@ func AddToNonTailTest(t *testing.T, factory Factory) { } // Adding to the previous preference will update the preference - sm.Add(firstBlock) - - if pref := sm.Preference(); !pref.Equals(firstBlock.id) { + if err := sm.Add(firstBlock); err != nil { + t.Fatal(err) + } else if pref := sm.Preference(); !pref.Equals(firstBlock.id) { t.Fatalf("Wrong preference. Expected %s, got %s", firstBlock.id, pref) } // Adding to something other than the previous preference won't update the // preference - sm.Add(secondBlock) - - if pref := sm.Preference(); !pref.Equals(firstBlock.id) { + if err := sm.Add(secondBlock); err != nil { + t.Fatal(err) + } else if pref := sm.Preference(); !pref.Equals(firstBlock.id) { t.Fatalf("Wrong preference. Expected %s, got %s", firstBlock.id, pref) } } @@ -171,9 +174,9 @@ func AddToUnknownTest(t *testing.T, factory Factory) { // Adding a block with an unknown parent means the parent must have already // been rejected. Therefore the block should be immediately rejected - sm.Add(block) - - if pref := sm.Preference(); !pref.Equals(GenesisID) { + if err := sm.Add(block); err != nil { + t.Fatal(err) + } else if pref := sm.Preference(); !pref.Equals(GenesisID) { t.Fatalf("Wrong preference. Expected %s, got %s", GenesisID, pref) } else if status := block.Status(); status != choices.Rejected { t.Fatalf("Should have rejected the block") @@ -269,9 +272,9 @@ func IssuedIssuedTest(t *testing.T, factory Factory) { status: choices.Processing, } - sm.Add(block) - - if !sm.Issued(block) { + if err := sm.Add(block); err != nil { + t.Fatal(err) + } else if !sm.Issued(block) { t.Fatalf("Should have marked a pending block as having been issued") } } @@ -296,24 +299,23 @@ func RecordPollAcceptSingleBlockTest(t *testing.T, factory Factory) { status: choices.Processing, } - sm.Add(block) + if err := sm.Add(block); err != nil { + t.Fatal(err) + } votes := ids.Bag{} votes.Add(block.id) - - sm.RecordPoll(votes) - - if pref := sm.Preference(); !pref.Equals(block.id) { + if err := sm.RecordPoll(votes); err != nil { + t.Fatal(err) + } else if pref := sm.Preference(); !pref.Equals(block.id) { t.Fatalf("Preference returned the wrong block") } else if sm.Finalized() { t.Fatalf("Snowman instance finalized too soon") } else if status := block.Status(); status != choices.Processing { t.Fatalf("Block's status changed unexpectedly") - } - - sm.RecordPoll(votes) - - if pref := sm.Preference(); !pref.Equals(block.id) { + } else if err := sm.RecordPoll(votes); err != nil { + t.Fatal(err) + } else if pref := sm.Preference(); !pref.Equals(block.id) { t.Fatalf("Preference returned the wrong block") } else if !sm.Finalized() { t.Fatalf("Snowman instance didn't finalize") @@ -347,15 +349,18 @@ func RecordPollAcceptAndRejectTest(t *testing.T, factory Factory) { status: choices.Processing, } - sm.Add(firstBlock) - sm.Add(secondBlock) + if err := sm.Add(firstBlock); err != nil { + t.Fatal(err) + } else if err := sm.Add(secondBlock); err != nil { + t.Fatal(err) + } votes := ids.Bag{} votes.Add(firstBlock.id) - sm.RecordPoll(votes) - - if pref := sm.Preference(); !pref.Equals(firstBlock.id) { + if err := sm.RecordPoll(votes); err != nil { + t.Fatal(err) + } else if pref := sm.Preference(); !pref.Equals(firstBlock.id) { t.Fatalf("Preference returned the wrong block") } else if sm.Finalized() { t.Fatalf("Snowman instance finalized too soon") @@ -363,11 +368,9 @@ func RecordPollAcceptAndRejectTest(t *testing.T, factory Factory) { t.Fatalf("Block's status changed unexpectedly") } else if status := secondBlock.Status(); status != choices.Processing { t.Fatalf("Block's status changed unexpectedly") - } - - sm.RecordPoll(votes) - - if pref := sm.Preference(); !pref.Equals(firstBlock.id) { + } else if err := sm.RecordPoll(votes); err != nil { + t.Fatal(err) + } else if pref := sm.Preference(); !pref.Equals(firstBlock.id) { t.Fatalf("Preference returned the wrong block") } else if !sm.Finalized() { t.Fatalf("Snowman instance didn't finalize") @@ -394,9 +397,9 @@ func RecordPollWhenFinalizedTest(t *testing.T, factory Factory) { votes := ids.Bag{} votes.Add(GenesisID) - sm.RecordPoll(votes) - - if !sm.Finalized() { + if err := sm.RecordPoll(votes); err != nil { + t.Fatal(err) + } else if !sm.Finalized() { t.Fatalf("Consensus should still be finalized") } else if pref := sm.Preference(); !GenesisID.Equals(pref) { t.Fatalf("Wrong preference listed") @@ -433,9 +436,13 @@ func RecordPollRejectTransitivelyTest(t *testing.T, factory Factory) { status: choices.Processing, } - sm.Add(block0) - sm.Add(block1) - sm.Add(block2) + if err := sm.Add(block0); err != nil { + t.Fatal(err) + } else if err := sm.Add(block1); err != nil { + t.Fatal(err) + } else if err := sm.Add(block2); err != nil { + t.Fatal(err) + } // Current graph structure: // G @@ -447,7 +454,9 @@ func RecordPollRejectTransitivelyTest(t *testing.T, factory Factory) { votes := ids.Bag{} votes.Add(block0.id) - sm.RecordPoll(votes) + if err := sm.RecordPoll(votes); err != nil { + t.Fatal(err) + } // Current graph structure: // 0 @@ -457,9 +466,7 @@ func RecordPollRejectTransitivelyTest(t *testing.T, factory Factory) { t.Fatalf("Finalized too late") } else if pref := sm.Preference(); !block0.id.Equals(pref) { t.Fatalf("Wrong preference listed") - } - - if status := block0.Status(); status != choices.Accepted { + } else if status := block0.Status(); status != choices.Accepted { t.Fatalf("Wrong status returned") } else if status := block1.Status(); status != choices.Rejected { t.Fatalf("Wrong status returned") @@ -503,10 +510,15 @@ func RecordPollTransitivelyResetConfidenceTest(t *testing.T, factory Factory) { status: choices.Processing, } - sm.Add(block0) - sm.Add(block1) - sm.Add(block2) - sm.Add(block3) + if err := sm.Add(block0); err != nil { + t.Fatal(err) + } else if err := sm.Add(block1); err != nil { + t.Fatal(err) + } else if err := sm.Add(block2); err != nil { + t.Fatal(err) + } else if err := sm.Add(block3); err != nil { + t.Fatal(err) + } // Current graph structure: // G @@ -517,26 +529,24 @@ func RecordPollTransitivelyResetConfidenceTest(t *testing.T, factory Factory) { votesFor2 := ids.Bag{} votesFor2.Add(block2.id) - sm.RecordPoll(votesFor2) - - if sm.Finalized() { + if err := sm.RecordPoll(votesFor2); err != nil { + t.Fatal(err) + } else if sm.Finalized() { t.Fatalf("Finalized too early") } else if pref := sm.Preference(); !block2.id.Equals(pref) { t.Fatalf("Wrong preference listed") } emptyVotes := ids.Bag{} - sm.RecordPoll(emptyVotes) - - if sm.Finalized() { + if err := sm.RecordPoll(emptyVotes); err != nil { + t.Fatal(err) + } else if sm.Finalized() { t.Fatalf("Finalized too early") } else if pref := sm.Preference(); !block2.id.Equals(pref) { t.Fatalf("Wrong preference listed") - } - - sm.RecordPoll(votesFor2) - - if sm.Finalized() { + } else if err := sm.RecordPoll(votesFor2); err != nil { + t.Fatal(err) + } else if sm.Finalized() { t.Fatalf("Finalized too early") } else if pref := sm.Preference(); !block2.id.Equals(pref) { t.Fatalf("Wrong preference listed") @@ -544,23 +554,19 @@ func RecordPollTransitivelyResetConfidenceTest(t *testing.T, factory Factory) { votesFor3 := ids.Bag{} votesFor3.Add(block3.id) - sm.RecordPoll(votesFor3) - - if sm.Finalized() { + if err := sm.RecordPoll(votesFor3); err != nil { + t.Fatal(err) + } else if sm.Finalized() { t.Fatalf("Finalized too early") } else if pref := sm.Preference(); !block2.id.Equals(pref) { t.Fatalf("Wrong preference listed") - } - - sm.RecordPoll(votesFor3) - - if !sm.Finalized() { + } else if err := sm.RecordPoll(votesFor3); err != nil { + t.Fatal(err) + } else if !sm.Finalized() { t.Fatalf("Finalized too late") } else if pref := sm.Preference(); !block3.id.Equals(pref) { t.Fatalf("Wrong preference listed") - } - - if status := block0.Status(); status != choices.Rejected { + } else if status := block0.Status(); status != choices.Rejected { t.Fatalf("Wrong status returned") } else if status := block1.Status(); status != choices.Accepted { t.Fatalf("Wrong status returned") @@ -592,19 +598,23 @@ func RecordPollInvalidVoteTest(t *testing.T, factory Factory) { } unknownBlockID := ids.Empty.Prefix(2) - sm.Add(block) + if err := sm.Add(block); err != nil { + t.Fatal(err) + } validVotes := ids.Bag{} validVotes.Add(block.id) - sm.RecordPoll(validVotes) + if err := sm.RecordPoll(validVotes); err != nil { + t.Fatal(err) + } invalidVotes := ids.Bag{} invalidVotes.Add(unknownBlockID) - sm.RecordPoll(invalidVotes) - - sm.RecordPoll(validVotes) - - if sm.Finalized() { + if err := sm.RecordPoll(invalidVotes); err != nil { + t.Fatal(err) + } else if err := sm.RecordPoll(validVotes); err != nil { + t.Fatal(err) + } else if sm.Finalized() { t.Fatalf("Finalized too early") } else if pref := sm.Preference(); !block.id.Equals(pref) { t.Fatalf("Wrong preference listed") @@ -651,11 +661,17 @@ func RecordPollTransitiveVotingTest(t *testing.T, factory Factory) { status: choices.Processing, } - sm.Add(block0) - sm.Add(block1) - sm.Add(block2) - sm.Add(block3) - sm.Add(block4) + if err := sm.Add(block0); err != nil { + t.Fatal(err) + } else if err := sm.Add(block1); err != nil { + t.Fatal(err) + } else if err := sm.Add(block2); err != nil { + t.Fatal(err) + } else if err := sm.Add(block3); err != nil { + t.Fatal(err) + } else if err := sm.Add(block4); err != nil { + t.Fatal(err) + } // Current graph structure: // G @@ -668,10 +684,14 @@ func RecordPollTransitiveVotingTest(t *testing.T, factory Factory) { // Tail = 2 votes0_2_4 := ids.Bag{} - votes0_2_4.Add(block0.id) - votes0_2_4.Add(block2.id) - votes0_2_4.Add(block4.id) - sm.RecordPoll(votes0_2_4) + votes0_2_4.Add( + block0.id, + block2.id, + block4.id, + ) + if err := sm.RecordPoll(votes0_2_4); err != nil { + t.Fatal(err) + } // Current graph structure: // 0 @@ -699,7 +719,9 @@ func RecordPollTransitiveVotingTest(t *testing.T, factory Factory) { dep2_2_2 := ids.Bag{} dep2_2_2.AddCount(block2.id, 3) - sm.RecordPoll(dep2_2_2) + if err := sm.RecordPoll(dep2_2_2); err != nil { + t.Fatal(err) + } // Current graph structure: // 2 @@ -757,20 +779,25 @@ func RecordPollDivergedVotingTest(t *testing.T, factory Factory) { status: choices.Processing, } - sm.Add(block0) - sm.Add(block1) + if err := sm.Add(block0); err != nil { + t.Fatal(err) + } else if err := sm.Add(block1); err != nil { + t.Fatal(err) + } votes0 := ids.Bag{} votes0.Add(block0.id) - sm.RecordPoll(votes0) - - sm.Add(block2) + if err := sm.RecordPoll(votes0); err != nil { + t.Fatal(err) + } else if err := sm.Add(block2); err != nil { + t.Fatal(err) + } // dep2 is already rejected. - sm.Add(block3) - - if status := block0.Status(); status == choices.Accepted { + if err := sm.Add(block3); err != nil { + t.Fatal(err) + } else if status := block0.Status(); status == choices.Accepted { t.Fatalf("Shouldn't be accepted yet") } @@ -778,9 +805,9 @@ func RecordPollDivergedVotingTest(t *testing.T, factory Factory) { // dep0. Because dep2 is already rejected, this will accept dep0. votes3 := ids.Bag{} votes3.Add(block3.id) - sm.RecordPoll(votes3) - - if !sm.Finalized() { + if err := sm.RecordPoll(votes3); err != nil { + t.Fatal(err) + } else if !sm.Finalized() { t.Fatalf("Finalized too late") } else if status := block0.Status(); status != choices.Accepted { t.Fatalf("Should be accepted") @@ -818,14 +845,15 @@ func MetricsProcessingErrorTest(t *testing.T, factory Factory) { status: choices.Processing, } - sm.Add(block) + if err := sm.Add(block); err != nil { + t.Fatal(err) + } votes := ids.Bag{} votes.Add(block.id) - - sm.RecordPoll(votes) - - if !sm.Finalized() { + if err := sm.RecordPoll(votes); err != nil { + t.Fatal(err) + } else if !sm.Finalized() { t.Fatalf("Snowman instance didn't finalize") } } @@ -861,14 +889,15 @@ func MetricsAcceptedErrorTest(t *testing.T, factory Factory) { status: choices.Processing, } - sm.Add(block) + if err := sm.Add(block); err != nil { + t.Fatal(err) + } votes := ids.Bag{} votes.Add(block.id) - - sm.RecordPoll(votes) - - if !sm.Finalized() { + if err := sm.RecordPoll(votes); err != nil { + t.Fatal(err) + } else if !sm.Finalized() { t.Fatalf("Snowman instance didn't finalize") } } @@ -904,18 +933,171 @@ func MetricsRejectedErrorTest(t *testing.T, factory Factory) { status: choices.Processing, } - sm.Add(block) + if err := sm.Add(block); err != nil { + t.Fatal(err) + } votes := ids.Bag{} votes.Add(block.id) - - sm.RecordPoll(votes) - - if !sm.Finalized() { + if err := sm.RecordPoll(votes); err != nil { + t.Fatal(err) + } else if !sm.Finalized() { t.Fatalf("Snowman instance didn't finalize") } } +func ErrorOnInitialRejectionTest(t *testing.T, factory Factory) { + sm := factory.New() + + ctx := snow.DefaultContextTest() + params := snowball.Parameters{ + Metrics: prometheus.NewRegistry(), + K: 1, + Alpha: 1, + BetaVirtuous: 1, + BetaRogue: 1, + ConcurrentRepolls: 1, + } + + sm.Initialize(ctx, params, GenesisID) + + rejectedBlock := &TestBlock{ + id: ids.Empty.Prefix(1), + status: choices.Rejected, + } + + block := &TestBlock{ + parent: rejectedBlock, + id: ids.Empty.Prefix(2), + status: choices.Processing, + err: errors.New(""), + } + + if err := sm.Add(block); err == nil { + t.Fatalf("Should have errored on rejecting the rejectable block") + } +} + +func ErrorOnAcceptTest(t *testing.T, factory Factory) { + sm := factory.New() + + ctx := snow.DefaultContextTest() + params := snowball.Parameters{ + Metrics: prometheus.NewRegistry(), + K: 1, + Alpha: 1, + BetaVirtuous: 1, + BetaRogue: 1, + ConcurrentRepolls: 1, + } + + sm.Initialize(ctx, params, GenesisID) + + block := &TestBlock{ + parent: Genesis, + id: ids.Empty.Prefix(1), + status: choices.Processing, + err: errors.New(""), + } + + if err := sm.Add(block); err != nil { + t.Fatal(err) + } + + votes := ids.Bag{} + votes.Add(block.id) + if err := sm.RecordPoll(votes); err == nil { + t.Fatalf("Should have errored on accepted the block") + } +} + +func ErrorOnRejectSiblingTest(t *testing.T, factory Factory) { + sm := factory.New() + + ctx := snow.DefaultContextTest() + params := snowball.Parameters{ + Metrics: prometheus.NewRegistry(), + K: 1, + Alpha: 1, + BetaVirtuous: 1, + BetaRogue: 1, + ConcurrentRepolls: 1, + } + + sm.Initialize(ctx, params, GenesisID) + + block0 := &TestBlock{ + parent: Genesis, + id: ids.Empty.Prefix(1), + status: choices.Processing, + } + block1 := &TestBlock{ + parent: Genesis, + id: ids.Empty.Prefix(2), + status: choices.Processing, + err: errors.New(""), + } + + if err := sm.Add(block0); err != nil { + t.Fatal(err) + } else if err := sm.Add(block1); err != nil { + t.Fatal(err) + } + + votes := ids.Bag{} + votes.Add(block0.id) + if err := sm.RecordPoll(votes); err == nil { + t.Fatalf("Should have errored on rejecting the block's sibling") + } +} + +func ErrorOnTransitiveRejectionTest(t *testing.T, factory Factory) { + sm := factory.New() + + ctx := snow.DefaultContextTest() + params := snowball.Parameters{ + Metrics: prometheus.NewRegistry(), + K: 1, + Alpha: 1, + BetaVirtuous: 1, + BetaRogue: 1, + ConcurrentRepolls: 1, + } + + sm.Initialize(ctx, params, GenesisID) + + block0 := &TestBlock{ + parent: Genesis, + id: ids.Empty.Prefix(1), + status: choices.Processing, + } + block1 := &TestBlock{ + parent: Genesis, + id: ids.Empty.Prefix(2), + status: choices.Processing, + } + block2 := &TestBlock{ + parent: block1, + id: ids.Empty.Prefix(3), + status: choices.Processing, + err: errors.New(""), + } + + if err := sm.Add(block0); err != nil { + t.Fatal(err) + } else if err := sm.Add(block1); err != nil { + t.Fatal(err) + } else if err := sm.Add(block2); err != nil { + t.Fatal(err) + } + + votes := ids.Bag{} + votes.Add(block0.id) + if err := sm.RecordPoll(votes); err == nil { + t.Fatalf("Should have errored on transitively rejecting the block") + } +} + func RandomizedConsistencyTest(t *testing.T, factory Factory) { numColors := 50 numNodes := 100 diff --git a/snow/consensus/snowman/topological.go b/snow/consensus/snowman/topological.go index 6f98751..51612db 100644 --- a/snow/consensus/snowman/topological.go +++ b/snow/consensus/snowman/topological.go @@ -9,6 +9,10 @@ import ( "github.com/ava-labs/gecko/snow/consensus/snowball" ) +const ( + minMapSize = 16 +) + // TopologicalFactory implements Factory by returning a topological struct type TopologicalFactory struct{} @@ -183,7 +187,7 @@ func (ts *Topological) Finalized() bool { return len(ts.blocks) == 1 } // the non-transitively applied votes. Also returns the list of leaf blocks. func (ts *Topological) calculateInDegree( votes ids.Bag) (map[[32]byte]kahnNode, []ids.ID) { - kahns := make(map[[32]byte]kahnNode) + kahns := make(map[[32]byte]kahnNode, minMapSize) leaves := ids.Set{} for _, vote := range votes.List() { diff --git a/snow/consensus/snowstorm/consensus_test.go b/snow/consensus/snowstorm/consensus_test.go index 270292c..5ff46ab 100644 --- a/snow/consensus/snowstorm/consensus_test.go +++ b/snow/consensus/snowstorm/consensus_test.go @@ -4,6 +4,7 @@ package snowstorm import ( + "errors" "testing" "github.com/prometheus/client_golang/prometheus" @@ -19,6 +20,28 @@ var ( Green = &TestTx{Identifier: ids.Empty.Prefix(1)} Blue = &TestTx{Identifier: ids.Empty.Prefix(2)} Alpha = &TestTx{Identifier: ids.Empty.Prefix(3)} + + Tests = []func(*testing.T, Factory){ + MetricsTest, + ParamsTest, + IssuedTest, + LeftoverInputTest, + LowerConfidenceTest, + MiddleConfidenceTest, + IndependentTest, + VirtuousTest, + IsVirtuousTest, + QuiesceTest, + AcceptingDependencyTest, + RejectingDependencyTest, + VacuouslyAcceptedTest, + ConflictsTest, + VirtuousDependsOnRogueTest, + ErrorOnVacuouslyAcceptedTest, + ErrorOnAcceptedTest, + ErrorOnRejectingLowerConfidenceConflictTest, + ErrorOnRejectingHigherConfidenceConflictTest, + } ) // R - G - B - A @@ -46,6 +69,52 @@ func Setup() { Alpha.Reset() } +// Execute all tests against a consensus implementation +func ConsensusTest(t *testing.T, factory Factory, prefix string) { + for _, test := range Tests { + test(t, factory) + } + StringTest(t, factory, prefix) +} + +func MetricsTest(t *testing.T, factory Factory) { + Setup() + + { + params := snowball.Parameters{ + Metrics: prometheus.NewRegistry(), + K: 2, Alpha: 2, BetaVirtuous: 1, BetaRogue: 2, + } + params.Metrics.Register(prometheus.NewCounter(prometheus.CounterOpts{ + Name: "tx_processing", + })) + graph := factory.New() + graph.Initialize(snow.DefaultContextTest(), params) + } + { + params := snowball.Parameters{ + Metrics: prometheus.NewRegistry(), + K: 2, Alpha: 2, BetaVirtuous: 1, BetaRogue: 2, + } + params.Metrics.Register(prometheus.NewCounter(prometheus.CounterOpts{ + Name: "tx_accepted", + })) + graph := factory.New() + graph.Initialize(snow.DefaultContextTest(), params) + } + { + params := snowball.Parameters{ + Metrics: prometheus.NewRegistry(), + K: 2, Alpha: 2, BetaVirtuous: 1, BetaRogue: 2, + } + params.Metrics.Register(prometheus.NewCounter(prometheus.CounterOpts{ + Name: "tx_rejected", + })) + graph := factory.New() + graph.Initialize(snow.DefaultContextTest(), params) + } +} + func ParamsTest(t *testing.T, factory Factory) { Setup() @@ -81,15 +150,13 @@ func IssuedTest(t *testing.T, factory Factory) { if issued := graph.Issued(Red); issued { t.Fatalf("Haven't issued anything yet.") - } - - graph.Add(Red) - - if issued := graph.Issued(Red); !issued { + } else if err := graph.Add(Red); err != nil { + t.Fatal(err) + } else if issued := graph.Issued(Red); !issued { t.Fatalf("Have already issued.") } - Blue.Accept() + _ = Blue.Accept() if issued := graph.Issued(Blue); !issued { t.Fatalf("Have already accepted.") @@ -106,10 +173,12 @@ func LeftoverInputTest(t *testing.T, factory Factory) { K: 2, Alpha: 2, BetaVirtuous: 1, BetaRogue: 1, } graph.Initialize(snow.DefaultContextTest(), params) - graph.Add(Red) - graph.Add(Green) - if prefs := graph.Preferences(); prefs.Len() != 1 { + if err := graph.Add(Red); err != nil { + t.Fatal(err) + } else if err := graph.Add(Green); err != nil { + t.Fatal(err) + } else if prefs := graph.Preferences(); prefs.Len() != 1 { t.Fatalf("Wrong number of preferences.") } else if !prefs.Contains(Red.ID()) { t.Fatalf("Wrong preference. Expected %s got %s", Red.ID(), prefs.List()[0]) @@ -120,15 +189,13 @@ func LeftoverInputTest(t *testing.T, factory Factory) { r := ids.Bag{} r.SetThreshold(2) r.AddCount(Red.ID(), 2) - graph.RecordPoll(r) - - if prefs := graph.Preferences(); prefs.Len() != 0 { + if err := graph.RecordPoll(r); err != nil { + t.Fatal(err) + } else if prefs := graph.Preferences(); prefs.Len() != 0 { t.Fatalf("Wrong number of preferences.") } else if !graph.Finalized() { t.Fatalf("Finalized too late") - } - - if Red.Status() != choices.Accepted { + } else if Red.Status() != choices.Accepted { t.Fatalf("%s should have been accepted", Red.ID()) } else if Green.Status() != choices.Rejected { t.Fatalf("%s should have been rejected", Green.ID()) @@ -145,11 +212,14 @@ func LowerConfidenceTest(t *testing.T, factory Factory) { K: 2, Alpha: 2, BetaVirtuous: 1, BetaRogue: 1, } graph.Initialize(snow.DefaultContextTest(), params) - graph.Add(Red) - graph.Add(Green) - graph.Add(Blue) - if prefs := graph.Preferences(); prefs.Len() != 1 { + if err := graph.Add(Red); err != nil { + t.Fatal(err) + } else if err := graph.Add(Green); err != nil { + t.Fatal(err) + } else if err := graph.Add(Blue); err != nil { + t.Fatal(err) + } else if prefs := graph.Preferences(); prefs.Len() != 1 { t.Fatalf("Wrong number of preferences.") } else if !prefs.Contains(Red.ID()) { t.Fatalf("Wrong preference. Expected %s got %s", Red.ID(), prefs.List()[0]) @@ -160,9 +230,9 @@ func LowerConfidenceTest(t *testing.T, factory Factory) { r := ids.Bag{} r.SetThreshold(2) r.AddCount(Red.ID(), 2) - graph.RecordPoll(r) - - if prefs := graph.Preferences(); prefs.Len() != 1 { + if err := graph.RecordPoll(r); err != nil { + t.Fatal(err) + } else if prefs := graph.Preferences(); prefs.Len() != 1 { t.Fatalf("Wrong number of preferences.") } else if !prefs.Contains(Blue.ID()) { t.Fatalf("Wrong preference. Expected %s", Blue.ID()) @@ -181,12 +251,16 @@ func MiddleConfidenceTest(t *testing.T, factory Factory) { K: 2, Alpha: 2, BetaVirtuous: 1, BetaRogue: 1, } graph.Initialize(snow.DefaultContextTest(), params) - graph.Add(Red) - graph.Add(Green) - graph.Add(Alpha) - graph.Add(Blue) - if prefs := graph.Preferences(); prefs.Len() != 2 { + if err := graph.Add(Red); err != nil { + t.Fatal(err) + } else if err := graph.Add(Green); err != nil { + t.Fatal(err) + } else if err := graph.Add(Alpha); err != nil { + t.Fatal(err) + } else if err := graph.Add(Blue); err != nil { + t.Fatal(err) + } else if prefs := graph.Preferences(); prefs.Len() != 2 { t.Fatalf("Wrong number of preferences.") } else if !prefs.Contains(Red.ID()) { t.Fatalf("Wrong preference. Expected %s", Red.ID()) @@ -199,9 +273,9 @@ func MiddleConfidenceTest(t *testing.T, factory Factory) { r := ids.Bag{} r.SetThreshold(2) r.AddCount(Red.ID(), 2) - graph.RecordPoll(r) - - if prefs := graph.Preferences(); prefs.Len() != 1 { + if err := graph.RecordPoll(r); err != nil { + t.Fatal(err) + } else if prefs := graph.Preferences(); prefs.Len() != 1 { t.Fatalf("Wrong number of preferences.") } else if !prefs.Contains(Alpha.ID()) { t.Fatalf("Wrong preference. Expected %s", Alpha.ID()) @@ -209,6 +283,7 @@ func MiddleConfidenceTest(t *testing.T, factory Factory) { t.Fatalf("Finalized too early") } } + func IndependentTest(t *testing.T, factory Factory) { Setup() @@ -219,10 +294,12 @@ func IndependentTest(t *testing.T, factory Factory) { K: 2, Alpha: 2, BetaVirtuous: 2, BetaRogue: 2, } graph.Initialize(snow.DefaultContextTest(), params) - graph.Add(Red) - graph.Add(Alpha) - if prefs := graph.Preferences(); prefs.Len() != 2 { + if err := graph.Add(Red); err != nil { + t.Fatal(err) + } else if err := graph.Add(Alpha); err != nil { + t.Fatal(err) + } else if prefs := graph.Preferences(); prefs.Len() != 2 { t.Fatalf("Wrong number of preferences.") } else if !prefs.Contains(Red.ID()) { t.Fatalf("Wrong preference. Expected %s", Red.ID()) @@ -236,9 +313,9 @@ func IndependentTest(t *testing.T, factory Factory) { ra.SetThreshold(2) ra.AddCount(Red.ID(), 2) ra.AddCount(Alpha.ID(), 2) - graph.RecordPoll(ra) - - if prefs := graph.Preferences(); prefs.Len() != 2 { + if err := graph.RecordPoll(ra); err != nil { + t.Fatal(err) + } else if prefs := graph.Preferences(); prefs.Len() != 2 { t.Fatalf("Wrong number of preferences.") } else if !prefs.Contains(Red.ID()) { t.Fatalf("Wrong preference. Expected %s", Red.ID()) @@ -246,11 +323,9 @@ func IndependentTest(t *testing.T, factory Factory) { t.Fatalf("Wrong preference. Expected %s", Alpha.ID()) } else if graph.Finalized() { t.Fatalf("Finalized too early") - } - - graph.RecordPoll(ra) - - if prefs := graph.Preferences(); prefs.Len() != 0 { + } else if err := graph.RecordPoll(ra); err != nil { + t.Fatal(err) + } else if prefs := graph.Preferences(); prefs.Len() != 0 { t.Fatalf("Wrong number of preferences.") } else if !graph.Finalized() { t.Fatalf("Finalized too late") @@ -267,35 +342,30 @@ func VirtuousTest(t *testing.T, factory Factory) { K: 2, Alpha: 2, BetaVirtuous: 1, BetaRogue: 1, } graph.Initialize(snow.DefaultContextTest(), params) - graph.Add(Red) - if virtuous := graph.Virtuous(); virtuous.Len() != 1 { + if err := graph.Add(Red); err != nil { + t.Fatal(err) + } else if virtuous := graph.Virtuous(); virtuous.Len() != 1 { t.Fatalf("Wrong number of virtuous.") } else if !virtuous.Contains(Red.ID()) { t.Fatalf("Wrong virtuous. Expected %s", Red.ID()) - } - - graph.Add(Alpha) - - if virtuous := graph.Virtuous(); virtuous.Len() != 2 { + } else if err := graph.Add(Alpha); err != nil { + t.Fatal(err) + } else if virtuous := graph.Virtuous(); virtuous.Len() != 2 { t.Fatalf("Wrong number of virtuous.") } else if !virtuous.Contains(Red.ID()) { t.Fatalf("Wrong virtuous. Expected %s", Red.ID()) } else if !virtuous.Contains(Alpha.ID()) { t.Fatalf("Wrong virtuous. Expected %s", Alpha.ID()) - } - - graph.Add(Green) - - if virtuous := graph.Virtuous(); virtuous.Len() != 1 { + } else if err := graph.Add(Green); err != nil { + t.Fatal(err) + } else if virtuous := graph.Virtuous(); virtuous.Len() != 1 { t.Fatalf("Wrong number of virtuous.") } else if !virtuous.Contains(Alpha.ID()) { t.Fatalf("Wrong virtuous. Expected %s", Alpha.ID()) - } - - graph.Add(Blue) - - if virtuous := graph.Virtuous(); virtuous.Len() != 0 { + } else if err := graph.Add(Blue); err != nil { + t.Fatal(err) + } else if virtuous := graph.Virtuous(); virtuous.Len() != 0 { t.Fatalf("Wrong number of virtuous.") } } @@ -319,11 +389,9 @@ func IsVirtuousTest(t *testing.T, factory Factory) { t.Fatalf("Should be virtuous") } else if !graph.IsVirtuous(Alpha) { t.Fatalf("Should be virtuous") - } - - graph.Add(Red) - - if !graph.IsVirtuous(Red) { + } else if err := graph.Add(Red); err != nil { + t.Fatal(err) + } else if !graph.IsVirtuous(Red) { t.Fatalf("Should be virtuous") } else if graph.IsVirtuous(Green) { t.Fatalf("Should not be virtuous") @@ -331,11 +399,9 @@ func IsVirtuousTest(t *testing.T, factory Factory) { t.Fatalf("Should be virtuous") } else if !graph.IsVirtuous(Alpha) { t.Fatalf("Should be virtuous") - } - - graph.Add(Green) - - if graph.IsVirtuous(Red) { + } else if err := graph.Add(Green); err != nil { + t.Fatal(err) + } else if graph.IsVirtuous(Red) { t.Fatalf("Should not be virtuous") } else if graph.IsVirtuous(Green) { t.Fatalf("Should not be virtuous") @@ -357,17 +423,13 @@ func QuiesceTest(t *testing.T, factory Factory) { if !graph.Quiesce() { t.Fatalf("Should quiesce") - } - - graph.Add(Red) - - if graph.Quiesce() { + } else if err := graph.Add(Red); err != nil { + t.Fatal(err) + } else if graph.Quiesce() { t.Fatalf("Shouldn't quiesce") - } - - graph.Add(Green) - - if !graph.Quiesce() { + } else if err := graph.Add(Green); err != nil { + t.Fatal(err) + } else if !graph.Quiesce() { t.Fatalf("Should quiesce") } } @@ -390,11 +452,13 @@ func AcceptingDependencyTest(t *testing.T, factory Factory) { } graph.Initialize(snow.DefaultContextTest(), params) - graph.Add(Red) - graph.Add(Green) - graph.Add(purple) - - if prefs := graph.Preferences(); prefs.Len() != 2 { + if err := graph.Add(Red); err != nil { + t.Fatal(err) + } else if err := graph.Add(Green); err != nil { + t.Fatal(err) + } else if err := graph.Add(purple); err != nil { + t.Fatal(err) + } else if prefs := graph.Preferences(); prefs.Len() != 2 { t.Fatalf("Wrong number of preferences.") } else if !prefs.Contains(Red.ID()) { t.Fatalf("Wrong preference. Expected %s", Red.ID()) @@ -410,10 +474,9 @@ func AcceptingDependencyTest(t *testing.T, factory Factory) { g := ids.Bag{} g.Add(Green.ID()) - - graph.RecordPoll(g) - - if prefs := graph.Preferences(); prefs.Len() != 2 { + if err := graph.RecordPoll(g); err != nil { + t.Fatal(err) + } else if prefs := graph.Preferences(); prefs.Len() != 2 { t.Fatalf("Wrong number of preferences.") } else if !prefs.Contains(Green.ID()) { t.Fatalf("Wrong preference. Expected %s", Green.ID()) @@ -429,10 +492,9 @@ func AcceptingDependencyTest(t *testing.T, factory Factory) { rp := ids.Bag{} rp.Add(Red.ID(), purple.ID()) - - graph.RecordPoll(rp) - - if prefs := graph.Preferences(); prefs.Len() != 2 { + if err := graph.RecordPoll(rp); err != nil { + t.Fatal(err) + } else if prefs := graph.Preferences(); prefs.Len() != 2 { t.Fatalf("Wrong number of preferences.") } else if !prefs.Contains(Green.ID()) { t.Fatalf("Wrong preference. Expected %s", Green.ID()) @@ -448,10 +510,9 @@ func AcceptingDependencyTest(t *testing.T, factory Factory) { r := ids.Bag{} r.Add(Red.ID()) - - graph.RecordPoll(r) - - if prefs := graph.Preferences(); prefs.Len() != 0 { + if err := graph.RecordPoll(r); err != nil { + t.Fatal(err) + } else if prefs := graph.Preferences(); prefs.Len() != 0 { t.Fatalf("Wrong number of preferences.") } else if Red.Status() != choices.Accepted { t.Fatalf("Wrong status. %s should be %s", Red.ID(), choices.Accepted) @@ -480,12 +541,15 @@ func RejectingDependencyTest(t *testing.T, factory Factory) { } graph.Initialize(snow.DefaultContextTest(), params) - graph.Add(Red) - graph.Add(Green) - graph.Add(Blue) - graph.Add(purple) - - if prefs := graph.Preferences(); prefs.Len() != 2 { + if err := graph.Add(Red); err != nil { + t.Fatal(err) + } else if err := graph.Add(Green); err != nil { + t.Fatal(err) + } else if err := graph.Add(Blue); err != nil { + t.Fatal(err) + } else if err := graph.Add(purple); err != nil { + t.Fatal(err) + } else if prefs := graph.Preferences(); prefs.Len() != 2 { t.Fatalf("Wrong number of preferences.") } else if !prefs.Contains(Red.ID()) { t.Fatalf("Wrong preference. Expected %s", Red.ID()) @@ -503,10 +567,9 @@ func RejectingDependencyTest(t *testing.T, factory Factory) { gp := ids.Bag{} gp.Add(Green.ID(), purple.ID()) - - graph.RecordPoll(gp) - - if prefs := graph.Preferences(); prefs.Len() != 2 { + if err := graph.RecordPoll(gp); err != nil { + t.Fatal(err) + } else if prefs := graph.Preferences(); prefs.Len() != 2 { t.Fatalf("Wrong number of preferences.") } else if !prefs.Contains(Green.ID()) { t.Fatalf("Wrong preference. Expected %s", Green.ID()) @@ -520,11 +583,9 @@ func RejectingDependencyTest(t *testing.T, factory Factory) { t.Fatalf("Wrong status. %s should be %s", Blue.ID(), choices.Processing) } else if purple.Status() != choices.Processing { t.Fatalf("Wrong status. %s should be %s", purple.ID(), choices.Processing) - } - - graph.RecordPoll(gp) - - if prefs := graph.Preferences(); prefs.Len() != 0 { + } else if err := graph.RecordPoll(gp); err != nil { + t.Fatal(err) + } else if prefs := graph.Preferences(); prefs.Len() != 0 { t.Fatalf("Wrong number of preferences.") } else if Red.Status() != choices.Rejected { t.Fatalf("Wrong status. %s should be %s", Red.ID(), choices.Rejected) @@ -553,9 +614,9 @@ func VacuouslyAcceptedTest(t *testing.T, factory Factory) { } graph.Initialize(snow.DefaultContextTest(), params) - graph.Add(purple) - - if prefs := graph.Preferences(); prefs.Len() != 0 { + if err := graph.Add(purple); err != nil { + t.Fatal(err) + } else if prefs := graph.Preferences(); prefs.Len() != 0 { t.Fatalf("Wrong number of preferences.") } else if status := purple.Status(); status != choices.Accepted { t.Fatalf("Wrong status. %s should be %s", purple.ID(), choices.Accepted) @@ -593,17 +654,15 @@ func ConflictsTest(t *testing.T, factory Factory) { Ins: insPurple, } - graph.Add(purple) - - if orangeConflicts := graph.Conflicts(orange); orangeConflicts.Len() != 1 { + if err := graph.Add(purple); err != nil { + t.Fatal(err) + } else if orangeConflicts := graph.Conflicts(orange); orangeConflicts.Len() != 1 { t.Fatalf("Wrong number of conflicts") } else if !orangeConflicts.Contains(purple.Identifier) { t.Fatalf("Conflicts does not contain the right transaction") - } - - graph.Add(orange) - - if orangeConflicts := graph.Conflicts(orange); orangeConflicts.Len() != 1 { + } else if err := graph.Add(orange); err != nil { + t.Fatal(err) + } else if orangeConflicts := graph.Conflicts(orange); orangeConflicts.Len() != 1 { t.Fatalf("Wrong number of conflicts") } else if !orangeConflicts.Contains(purple.Identifier) { t.Fatalf("Conflicts does not contain the right transaction") @@ -643,17 +702,20 @@ func VirtuousDependsOnRogueTest(t *testing.T, factory Factory) { virtuous.Ins.Add(input2) - graph.Add(rogue1) - graph.Add(rogue2) - graph.Add(virtuous) + if err := graph.Add(rogue1); err != nil { + t.Fatal(err) + } else if err := graph.Add(rogue2); err != nil { + t.Fatal(err) + } else if err := graph.Add(virtuous); err != nil { + t.Fatal(err) + } votes := ids.Bag{} votes.Add(rogue1.ID()) votes.Add(virtuous.ID()) - - graph.RecordPoll(votes) - - if status := rogue1.Status(); status != choices.Processing { + if err := graph.RecordPoll(votes); err != nil { + t.Fatal(err) + } else if status := rogue1.Status(); status != choices.Processing { t.Fatalf("Rogue Tx is %s expected %s", status, choices.Processing) } else if status := rogue2.Status(); status != choices.Processing { t.Fatalf("Rogue Tx is %s expected %s", status, choices.Processing) @@ -664,6 +726,135 @@ func VirtuousDependsOnRogueTest(t *testing.T, factory Factory) { } } +func ErrorOnVacuouslyAcceptedTest(t *testing.T, factory Factory) { + Setup() + + graph := factory.New() + + purple := &TestTx{ + Identifier: ids.Empty.Prefix(7), + Stat: choices.Processing, + Validity: errors.New(""), + } + + params := snowball.Parameters{ + Metrics: prometheus.NewRegistry(), + K: 1, Alpha: 1, BetaVirtuous: 1, BetaRogue: 2, + } + graph.Initialize(snow.DefaultContextTest(), params) + + if err := graph.Add(purple); err == nil { + t.Fatalf("Should have errored on acceptance") + } +} + +func ErrorOnAcceptedTest(t *testing.T, factory Factory) { + Setup() + + graph := factory.New() + + purple := &TestTx{ + Identifier: ids.Empty.Prefix(7), + Stat: choices.Processing, + Validity: errors.New(""), + } + purple.Ins.Add(ids.Empty.Prefix(4)) + + params := snowball.Parameters{ + Metrics: prometheus.NewRegistry(), + K: 1, Alpha: 1, BetaVirtuous: 1, BetaRogue: 2, + } + graph.Initialize(snow.DefaultContextTest(), params) + + if err := graph.Add(purple); err != nil { + t.Fatal(err) + } + + votes := ids.Bag{} + votes.Add(purple.ID()) + if err := graph.RecordPoll(votes); err == nil { + t.Fatalf("Should have errored on accepting an invalid tx") + } +} + +func ErrorOnRejectingLowerConfidenceConflictTest(t *testing.T, factory Factory) { + Setup() + + graph := factory.New() + + X := ids.Empty.Prefix(4) + + purple := &TestTx{ + Identifier: ids.Empty.Prefix(7), + Stat: choices.Processing, + } + purple.Ins.Add(X) + + pink := &TestTx{ + Identifier: ids.Empty.Prefix(8), + Stat: choices.Processing, + Validity: errors.New(""), + } + pink.Ins.Add(X) + + params := snowball.Parameters{ + Metrics: prometheus.NewRegistry(), + K: 1, Alpha: 1, BetaVirtuous: 1, BetaRogue: 1, + } + graph.Initialize(snow.DefaultContextTest(), params) + + if err := graph.Add(purple); err != nil { + t.Fatal(err) + } else if err := graph.Add(pink); err != nil { + t.Fatal(err) + } + + votes := ids.Bag{} + votes.Add(purple.ID()) + if err := graph.RecordPoll(votes); err == nil { + t.Fatalf("Should have errored on rejecting an invalid tx") + } +} + +func ErrorOnRejectingHigherConfidenceConflictTest(t *testing.T, factory Factory) { + Setup() + + graph := factory.New() + + X := ids.Empty.Prefix(4) + + purple := &TestTx{ + Identifier: ids.Empty.Prefix(7), + Stat: choices.Processing, + } + purple.Ins.Add(X) + + pink := &TestTx{ + Identifier: ids.Empty.Prefix(8), + Stat: choices.Processing, + Validity: errors.New(""), + } + pink.Ins.Add(X) + + params := snowball.Parameters{ + Metrics: prometheus.NewRegistry(), + K: 1, Alpha: 1, BetaVirtuous: 1, BetaRogue: 1, + } + graph.Initialize(snow.DefaultContextTest(), params) + + if err := graph.Add(pink); err != nil { + t.Fatal(err) + } else if err := graph.Add(purple); err != nil { + t.Fatal(err) + } + + votes := ids.Bag{} + votes.Add(purple.ID()) + if err := graph.RecordPoll(votes); err == nil { + t.Fatalf("Should have errored on rejecting an invalid tx") + } +} + func StringTest(t *testing.T, factory Factory, prefix string) { Setup() @@ -674,12 +865,16 @@ func StringTest(t *testing.T, factory Factory, prefix string) { K: 2, Alpha: 2, BetaVirtuous: 1, BetaRogue: 2, } graph.Initialize(snow.DefaultContextTest(), params) - graph.Add(Red) - graph.Add(Green) - graph.Add(Blue) - graph.Add(Alpha) - if prefs := graph.Preferences(); prefs.Len() != 1 { + if err := graph.Add(Red); err != nil { + t.Fatal(err) + } else if err := graph.Add(Green); err != nil { + t.Fatal(err) + } else if err := graph.Add(Blue); err != nil { + t.Fatal(err) + } else if err := graph.Add(Alpha); err != nil { + t.Fatal(err) + } else if prefs := graph.Preferences(); prefs.Len() != 1 { t.Fatalf("Wrong number of preferences.") } else if !prefs.Contains(Red.ID()) { t.Fatalf("Wrong preference. Expected %s got %s", Red.ID(), prefs.List()[0]) @@ -691,8 +886,11 @@ func StringTest(t *testing.T, factory Factory, prefix string) { rb.SetThreshold(2) rb.AddCount(Red.ID(), 2) rb.AddCount(Blue.ID(), 2) - graph.RecordPoll(rb) - graph.Add(Blue) + if err := graph.RecordPoll(rb); err != nil { + t.Fatal(err) + } else if err := graph.Add(Blue); err != nil { + t.Fatal(err) + } { expected := prefix + "(\n" + @@ -720,7 +918,9 @@ func StringTest(t *testing.T, factory Factory, prefix string) { ga.SetThreshold(2) ga.AddCount(Green.ID(), 2) ga.AddCount(Alpha.ID(), 2) - graph.RecordPoll(ga) + if err := graph.RecordPoll(ga); err != nil { + t.Fatal(err) + } { expected := prefix + "(\n" + @@ -745,7 +945,9 @@ func StringTest(t *testing.T, factory Factory, prefix string) { } empty := ids.Bag{} - graph.RecordPoll(empty) + if err := graph.RecordPoll(empty); err != nil { + t.Fatal(err) + } { expected := prefix + "(\n" + @@ -767,10 +969,10 @@ func StringTest(t *testing.T, factory Factory, prefix string) { t.Fatalf("Wrong preference. Expected %s", Blue.ID()) } else if graph.Finalized() { t.Fatalf("Finalized too early") + } else if err := graph.RecordPoll(ga); err != nil { + t.Fatal(err) } - graph.RecordPoll(ga) - { expected := prefix + "(\n" + " Choice[0] = ID: LUC1cmcxnfNR9LdkACS2ccGKLEK7SYqB4gLLTycQfg1koyfSq Confidence: 0 Bias: 1\n" + @@ -791,10 +993,10 @@ func StringTest(t *testing.T, factory Factory, prefix string) { t.Fatalf("Wrong preference. Expected %s", Alpha.ID()) } else if graph.Finalized() { t.Fatalf("Finalized too early") + } else if err := graph.RecordPoll(ga); err != nil { + t.Fatal(err) } - graph.RecordPoll(ga) - { expected := prefix + "()" if str := graph.String(); str != expected { @@ -806,9 +1008,7 @@ func StringTest(t *testing.T, factory Factory, prefix string) { t.Fatalf("Wrong number of preferences.") } else if !graph.Finalized() { t.Fatalf("Finalized too late") - } - - if Green.Status() != choices.Accepted { + } else if Green.Status() != choices.Accepted { t.Fatalf("%s should have been accepted", Green.ID()) } else if Alpha.Status() != choices.Accepted { t.Fatalf("%s should have been accepted", Alpha.ID()) @@ -816,10 +1016,10 @@ func StringTest(t *testing.T, factory Factory, prefix string) { t.Fatalf("%s should have been rejected", Red.ID()) } else if Blue.Status() != choices.Rejected { t.Fatalf("%s should have been rejected", Blue.ID()) + } else if err := graph.RecordPoll(rb); err != nil { + t.Fatal(err) } - graph.RecordPoll(rb) - { expected := prefix + "()" if str := graph.String(); str != expected { @@ -831,9 +1031,7 @@ func StringTest(t *testing.T, factory Factory, prefix string) { t.Fatalf("Wrong number of preferences.") } else if !graph.Finalized() { t.Fatalf("Finalized too late") - } - - if Green.Status() != choices.Accepted { + } else if Green.Status() != choices.Accepted { t.Fatalf("%s should have been accepted", Green.ID()) } else if Alpha.Status() != choices.Accepted { t.Fatalf("%s should have been accepted", Alpha.ID()) diff --git a/snow/consensus/snowstorm/directed_test.go b/snow/consensus/snowstorm/directed_test.go index 39bc5bf..f61d53c 100644 --- a/snow/consensus/snowstorm/directed_test.go +++ b/snow/consensus/snowstorm/directed_test.go @@ -7,34 +7,4 @@ import ( "testing" ) -func TestDirectedParams(t *testing.T) { ParamsTest(t, DirectedFactory{}) } - -func TestDirectedIssued(t *testing.T) { IssuedTest(t, DirectedFactory{}) } - -func TestDirectedLeftoverInput(t *testing.T) { LeftoverInputTest(t, DirectedFactory{}) } - -func TestDirectedLowerConfidence(t *testing.T) { LowerConfidenceTest(t, DirectedFactory{}) } - -func TestDirectedMiddleConfidence(t *testing.T) { MiddleConfidenceTest(t, DirectedFactory{}) } - -func TestDirectedIndependent(t *testing.T) { IndependentTest(t, DirectedFactory{}) } - -func TestDirectedVirtuous(t *testing.T) { VirtuousTest(t, DirectedFactory{}) } - -func TestDirectedIsVirtuous(t *testing.T) { IsVirtuousTest(t, DirectedFactory{}) } - -func TestDirectedConflicts(t *testing.T) { ConflictsTest(t, DirectedFactory{}) } - -func TestDirectedQuiesce(t *testing.T) { QuiesceTest(t, DirectedFactory{}) } - -func TestDirectedAcceptingDependency(t *testing.T) { AcceptingDependencyTest(t, DirectedFactory{}) } - -func TestDirectedRejectingDependency(t *testing.T) { RejectingDependencyTest(t, DirectedFactory{}) } - -func TestDirectedVacuouslyAccepted(t *testing.T) { VacuouslyAcceptedTest(t, DirectedFactory{}) } - -func TestDirectedVirtuousDependsOnRogue(t *testing.T) { - VirtuousDependsOnRogueTest(t, DirectedFactory{}) -} - -func TestDirectedString(t *testing.T) { StringTest(t, DirectedFactory{}, "DG") } +func TestDirectedConsensus(t *testing.T) { ConsensusTest(t, DirectedFactory{}, "DG") } diff --git a/snow/consensus/snowstorm/input_test.go b/snow/consensus/snowstorm/input_test.go index 46a0033..9cae5e7 100644 --- a/snow/consensus/snowstorm/input_test.go +++ b/snow/consensus/snowstorm/input_test.go @@ -7,32 +7,4 @@ import ( "testing" ) -func TestInputParams(t *testing.T) { ParamsTest(t, InputFactory{}) } - -func TestInputIssued(t *testing.T) { IssuedTest(t, InputFactory{}) } - -func TestInputLeftoverInput(t *testing.T) { LeftoverInputTest(t, InputFactory{}) } - -func TestInputLowerConfidence(t *testing.T) { LowerConfidenceTest(t, InputFactory{}) } - -func TestInputMiddleConfidence(t *testing.T) { MiddleConfidenceTest(t, InputFactory{}) } - -func TestInputIndependent(t *testing.T) { IndependentTest(t, InputFactory{}) } - -func TestInputVirtuous(t *testing.T) { VirtuousTest(t, InputFactory{}) } - -func TestInputIsVirtuous(t *testing.T) { IsVirtuousTest(t, InputFactory{}) } - -func TestInputConflicts(t *testing.T) { ConflictsTest(t, InputFactory{}) } - -func TestInputQuiesce(t *testing.T) { QuiesceTest(t, InputFactory{}) } - -func TestInputAcceptingDependency(t *testing.T) { AcceptingDependencyTest(t, InputFactory{}) } - -func TestInputRejectingDependency(t *testing.T) { RejectingDependencyTest(t, InputFactory{}) } - -func TestInputVacuouslyAccepted(t *testing.T) { VacuouslyAcceptedTest(t, InputFactory{}) } - -func TestInputVirtuousDependsOnRogue(t *testing.T) { VirtuousDependsOnRogueTest(t, InputFactory{}) } - -func TestInputString(t *testing.T) { StringTest(t, InputFactory{}, "IG") } +func TestInputConsensus(t *testing.T) { ConsensusTest(t, InputFactory{}, "IG") } diff --git a/snow/consensus/snowstorm/test_tx.go b/snow/consensus/snowstorm/test_tx.go index 18f1465..e9fb8f7 100644 --- a/snow/consensus/snowstorm/test_tx.go +++ b/snow/consensus/snowstorm/test_tx.go @@ -31,10 +31,10 @@ func (tx *TestTx) InputIDs() ids.Set { return tx.Ins } func (tx *TestTx) Status() choices.Status { return tx.Stat } // Accept implements the Consumer interface -func (tx *TestTx) Accept() error { tx.Stat = choices.Accepted; return nil } +func (tx *TestTx) Accept() error { tx.Stat = choices.Accepted; return tx.Validity } // Reject implements the Consumer interface -func (tx *TestTx) Reject() error { tx.Stat = choices.Rejected; return nil } +func (tx *TestTx) Reject() error { tx.Stat = choices.Rejected; return tx.Validity } // Reset sets the status to pending func (tx *TestTx) Reset() { tx.Stat = choices.Processing } diff --git a/snow/engine/avalanche/bootstrapper.go b/snow/engine/avalanche/bootstrapper.go index 705d841..200f84f 100644 --- a/snow/engine/avalanche/bootstrapper.go +++ b/snow/engine/avalanche/bootstrapper.go @@ -17,7 +17,12 @@ import ( ) const ( - cacheSize = 100000 + // We cache processed vertices where height = c * stripeDistance for c = {1,2,3...} + // This forms a "stripe" of cached DAG vertices at height stripeDistance, 2*stripeDistance, etc. + // This helps to limit the number of repeated DAG traversals performed + stripeDistance = 2000 + stripeWidth = 5 + cacheSize = 100000 ) // BootstrapConfig ... @@ -37,15 +42,16 @@ type bootstrapper struct { metrics common.Bootstrapper - // true if all of the vertices in the original accepted frontier have been processed - processedStartingAcceptedFrontier bool - // number of vertices fetched so far numFetched uint32 // tracks which validators were asked for which containers in which requests outstandingRequests common.Requests + // IDs of vertices that we will send a GetAncestors request for once we are + // not at the max number of outstanding requests + needToFetch ids.Set + // Contains IDs of vertices that have recently been processed processedCache *cache.LRU @@ -80,14 +86,15 @@ func (b *bootstrapper) Initialize(config BootstrapConfig) error { return nil } -// CurrentAcceptedFrontier ... +// CurrentAcceptedFrontier returns the set of vertices that this node has accepted +// that have no accepted children func (b *bootstrapper) CurrentAcceptedFrontier() ids.Set { acceptedFrontier := ids.Set{} acceptedFrontier.Add(b.State.Edge()...) return acceptedFrontier } -// FilterAccepted ... +// FilterAccepted returns the IDs of vertices in [containerIDs] that this node has accepted func (b *bootstrapper) FilterAccepted(containerIDs ids.Set) ids.Set { acceptedVtxIDs := ids.Set{} for _, vtxID := range containerIDs.List() { @@ -98,50 +105,64 @@ func (b *bootstrapper) FilterAccepted(containerIDs ids.Set) ids.Set { return acceptedVtxIDs } -// Get vertex [vtxID] and its ancestors -func (b *bootstrapper) fetch(vtxID ids.ID) error { - // Make sure we haven't already requested this block - if b.outstandingRequests.Contains(vtxID) { - return nil - } +// Add the vertices in [vtxIDs] to the set of vertices that we need to fetch, +// and then fetch vertices (and their ancestors) until either there are no more +// to fetch or we are at the maximum number of outstanding requests. +func (b *bootstrapper) fetch(vtxIDs ...ids.ID) error { + b.needToFetch.Add(vtxIDs...) + for b.needToFetch.Len() > 0 && b.outstandingRequests.Len() < common.MaxOutstandingRequests { + vtxID := b.needToFetch.CappedList(1)[0] + b.needToFetch.Remove(vtxID) - // Make sure we don't already have this vertex - if _, err := b.State.GetVertex(vtxID); err == nil { - return nil - } - - validators := b.BootstrapConfig.Validators.Sample(1) // validator to send request to - if len(validators) == 0 { - return fmt.Errorf("Dropping request for %s as there are no validators", vtxID) - } - validatorID := validators[0].ID() - b.RequestID++ - - b.outstandingRequests.Add(validatorID, b.RequestID, vtxID) - b.BootstrapConfig.Sender.GetAncestors(validatorID, b.RequestID, vtxID) // request vertex and ancestors - return nil -} - -// Process vertices -func (b *bootstrapper) process(vtx avalanche.Vertex) error { - toProcess := []avalanche.Vertex{vtx} - for len(toProcess) > 0 { - newLen := len(toProcess) - 1 - vtx := toProcess[newLen] - toProcess = toProcess[:newLen] - if _, ok := b.processedCache.Get(vtx.ID()); ok { // already processed this + // Make sure we haven't already requested this vertex + if b.outstandingRequests.Contains(vtxID) { continue } + // Make sure we don't already have this vertex + if _, err := b.State.GetVertex(vtxID); err == nil { + continue + } + + validators := b.BootstrapConfig.Validators.Sample(1) // validator to send request to + if len(validators) == 0 { + return fmt.Errorf("Dropping request for %s as there are no validators", vtxID) + } + validatorID := validators[0].ID() + b.RequestID++ + + b.outstandingRequests.Add(validatorID, b.RequestID, vtxID) + b.BootstrapConfig.Sender.GetAncestors(validatorID, b.RequestID, vtxID) // request vertex and ancestors + } + return b.finish() +} + +// Process the vertices in [vtxs]. +func (b *bootstrapper) process(vtxs ...avalanche.Vertex) error { + // Vertices that we need to process. Store them in a heap for de-deduplication + // and so we always process vertices further down in the DAG first. This helps + // to reduce the number of repeated DAG traversals. + toProcess := newMaxVertexHeap() + for _, vtx := range vtxs { + if _, ok := b.processedCache.Get(vtx.ID()); !ok { // only process a vertex if we haven't already + toProcess.Push(vtx) + } + } + + for toProcess.Len() > 0 { // While there are unprocessed vertices + vtx := toProcess.Pop() // Get an unknown vertex or one furthest down the DAG + vtxID := vtx.ID() + switch vtx.Status() { case choices.Unknown: - if err := b.fetch(vtx.ID()); err != nil { - return err - } + b.needToFetch.Add(vtxID) // We don't have this vertex locally. Mark that we need to fetch it. case choices.Rejected: + b.needToFetch.Remove(vtxID) // We have this vertex locally. Mark that we don't need to fetch it. return fmt.Errorf("tried to accept %s even though it was previously rejected", vtx.ID()) case choices.Processing: - if err := b.VtxBlocked.Push(&vertexJob{ + b.needToFetch.Remove(vtxID) + + if err := b.VtxBlocked.Push(&vertexJob{ // Add to queue of vertices to execute when bootstrapping finishes. log: b.BootstrapConfig.Context.Log, numAccepted: b.numBSVtx, numDropped: b.numBSDroppedVtx, @@ -155,7 +176,7 @@ func (b *bootstrapper) process(vtx avalanche.Vertex) error { } else { b.BootstrapConfig.Context.Log.Verbo("couldn't push to vtxBlocked: %s", err) } - for _, tx := range vtx.Txs() { + for _, tx := range vtx.Txs() { // Add transactions to queue of transactions to execute when bootstrapping finishes. if err := b.TxBlocked.Push(&txJob{ log: b.BootstrapConfig.Context.Log, numAccepted: b.numBSTx, @@ -167,10 +188,14 @@ func (b *bootstrapper) process(vtx avalanche.Vertex) error { b.BootstrapConfig.Context.Log.Verbo("couldn't push to txBlocked: %s", err) } } - for _, parent := range vtx.Parents() { - toProcess = append(toProcess, parent) + for _, parent := range vtx.Parents() { // Process the parents of this vertex (traverse up the DAG) + if _, ok := b.processedCache.Get(parent.ID()); !ok { // But only if we haven't processed the parent + toProcess.Push(parent) + } + } + if vtx.Height()%stripeDistance < stripeWidth { // See comment for stripeDistance + b.processedCache.Put(vtx.ID(), nil) } - b.processedCache.Put(vtx.ID(), nil) } } @@ -181,10 +206,7 @@ func (b *bootstrapper) process(vtx avalanche.Vertex) error { return err } - if numPending := b.outstandingRequests.Len(); numPending == 0 && b.processedStartingAcceptedFrontier { - return b.finish() - } - return nil + return b.fetch() } // MultiPut handles the receipt of multiple containers. Should be received in response to a GetAncestors message to [vdr] @@ -200,12 +222,12 @@ func (b *bootstrapper) MultiPut(vdr ids.ShortID, requestID uint32, vtxs [][]byte // Make sure this is in response to a request we made neededVtxID, needed := b.outstandingRequests.Remove(vdr, requestID) - if !needed { // this message isn't in response to a request we made + if !needed { // this message isn't in response to a request we made, or is in response to a request that timed out b.BootstrapConfig.Context.Log.Debug("received unexpected MultiPut from %s with ID %d", vdr, requestID) return nil } - neededVtx, err := b.State.ParseVertex(vtxs[0]) // the vertex we requested + neededVtx, err := b.State.ParseVertex(vtxs[0]) // first vertex should be the one we requested in GetAncestors request if err != nil { b.BootstrapConfig.Context.Log.Debug("Failed to parse requested vertex %s: %w", neededVtxID, err) b.BootstrapConfig.Context.Log.Verbo("vertex: %s", formatting.DumpBytes{Bytes: vtxs[0]}) @@ -215,14 +237,20 @@ func (b *bootstrapper) MultiPut(vdr ids.ShortID, requestID uint32, vtxs [][]byte return b.fetch(neededVtxID) } - for _, vtxBytes := range vtxs { // Parse/persist all the vertices - if _, err := b.State.ParseVertex(vtxBytes); err != nil { // Persists the vtx + processVertices := make([]avalanche.Vertex, 1, len(vtxs)) // Process all of the vertices in this message + processVertices[0] = neededVtx + + for _, vtxBytes := range vtxs[1:] { // Parse/persist all the vertices + if vtx, err := b.State.ParseVertex(vtxBytes); err != nil { // Persists the vtx b.BootstrapConfig.Context.Log.Debug("Failed to parse vertex: %w", err) b.BootstrapConfig.Context.Log.Verbo("vertex: %s", formatting.DumpBytes{Bytes: vtxBytes}) + } else { + processVertices = append(processVertices, vtx) + b.needToFetch.Remove(vtx.ID()) // No need to fetch this vertex since we have it now } } - return b.process(neededVtx) + return b.process(processVertices...) } // GetAncestorsFailed is called when a GetAncestors message we sent fails @@ -236,43 +264,38 @@ func (b *bootstrapper) GetAncestorsFailed(vdr ids.ShortID, requestID uint32) err return b.fetch(vtxID) } -// ForceAccepted ... +// ForceAccepted starts bootstrapping. Process the vertices in [accepterContainerIDs]. func (b *bootstrapper) ForceAccepted(acceptedContainerIDs ids.Set) error { if err := b.VM.Bootstrapping(); err != nil { return fmt.Errorf("failed to notify VM that bootstrapping has started: %w", err) } + toProcess := make([]avalanche.Vertex, 0, acceptedContainerIDs.Len()) for _, vtxID := range acceptedContainerIDs.List() { if vtx, err := b.State.GetVertex(vtxID); err == nil { - if err := b.process(vtx); err != nil { - return err - } - } else if err := b.fetch(vtxID); err != nil { - return err + toProcess = append(toProcess, vtx) // Process this vertex. + } else { + b.needToFetch.Add(vtxID) // We don't have this vertex. Mark that we have to fetch it. } } - b.processedStartingAcceptedFrontier = true - - if numPending := b.outstandingRequests.Len(); numPending == 0 { - return b.finish() - } - return nil + return b.process(toProcess...) } // Finish bootstrapping func (b *bootstrapper) finish() error { - if b.finished { + // If there are outstanding requests for vertices or we still need to fetch vertices, we can't finish + if b.finished || b.outstandingRequests.Len() > 0 || b.needToFetch.Len() > 0 { return nil } - b.BootstrapConfig.Context.Log.Info("finished fetching vertices. executing transaction state transitions...") + b.BootstrapConfig.Context.Log.Info("finished fetching %d vertices. executing transaction state transitions...", + b.numFetched) if err := b.executeAll(b.TxBlocked, b.numBSBlockedTx); err != nil { return err } b.BootstrapConfig.Context.Log.Info("executing vertex state transitions...") - if err := b.executeAll(b.VtxBlocked, b.numBSBlockedVtx); err != nil { return err } @@ -307,5 +330,6 @@ func (b *bootstrapper) executeAll(jobs *queue.Jobs, numBlocked prometheus.Gauge) b.BootstrapConfig.Context.Log.Info("executed %d operations", numExecuted) } } + b.BootstrapConfig.Context.Log.Info("executed %d operations", numExecuted) return nil } diff --git a/snow/engine/avalanche/bootstrapper_test.go b/snow/engine/avalanche/bootstrapper_test.go index 85e3a6e..c488d11 100644 --- a/snow/engine/avalanche/bootstrapper_test.go +++ b/snow/engine/avalanche/bootstrapper_test.go @@ -805,3 +805,113 @@ func TestBootstrapperIncompleteMultiPut(t *testing.T) { t.Fatal("should be accepted") } } + +func TestBootstrapperFinalized(t *testing.T) { + config, peerID, sender, state, vm := newConfig(t) + + vtxID0 := ids.Empty.Prefix(0) + vtxID1 := ids.Empty.Prefix(1) + + vtxBytes0 := []byte{0} + vtxBytes1 := []byte{1} + + vtx0 := &Vtx{ + id: vtxID0, + height: 0, + status: choices.Unknown, + bytes: vtxBytes0, + } + vtx1 := &Vtx{ + id: vtxID1, + height: 1, + parents: []avalanche.Vertex{vtx0}, + status: choices.Unknown, + bytes: vtxBytes1, + } + + bs := bootstrapper{} + bs.metrics.Initialize(config.Context.Log, fmt.Sprintf("gecko_%s", config.Context.ChainID), prometheus.NewRegistry()) + bs.Initialize(config) + finished := new(bool) + bs.onFinished = func() error { *finished = true; return nil } + + acceptedIDs := ids.Set{} + acceptedIDs.Add(vtxID0) + acceptedIDs.Add(vtxID1) + + parsedVtx0 := false + parsedVtx1 := false + state.getVertex = func(vtxID ids.ID) (avalanche.Vertex, error) { + switch { + case vtxID.Equals(vtxID0): + if parsedVtx0 { + return vtx0, nil + } + return nil, errUnknownVertex + case vtxID.Equals(vtxID1): + if parsedVtx1 { + return vtx1, nil + } + return nil, errUnknownVertex + default: + t.Fatal(errUnknownVertex) + panic(errUnknownVertex) + } + } + state.parseVertex = func(vtxBytes []byte) (avalanche.Vertex, error) { + switch { + case bytes.Equal(vtxBytes, vtxBytes0): + vtx0.status = choices.Processing + parsedVtx0 = true + return vtx0, nil + case bytes.Equal(vtxBytes, vtxBytes1): + vtx1.status = choices.Processing + parsedVtx1 = true + return vtx1, nil + } + t.Fatal(errUnknownVertex) + return nil, errUnknownVertex + } + + requestIDs := map[[32]byte]uint32{} + sender.GetAncestorsF = func(vdr ids.ShortID, reqID uint32, vtxID ids.ID) { + if !vdr.Equals(peerID) { + t.Fatalf("Should have requested block from %s, requested from %s", peerID, vdr) + } + requestIDs[vtxID.Key()] = reqID + } + + vm.CantBootstrapping = false + + if err := bs.ForceAccepted(acceptedIDs); err != nil { // should request vtx0 and vtx1 + t.Fatal(err) + } + + reqID, ok := requestIDs[vtxID1.Key()] + if !ok { + t.Fatalf("should have requested vtx1") + } + + vm.CantBootstrapped = false + + if err := bs.MultiPut(peerID, reqID, [][]byte{vtxBytes1, vtxBytes0}); err != nil { + t.Fatal(err) + } + + reqID, ok = requestIDs[vtxID0.Key()] + if !ok { + t.Fatalf("should have requested vtx0") + } + + if err := bs.GetAncestorsFailed(peerID, reqID); err != nil { + t.Fatal(err) + } + + if !*finished { + t.Fatalf("Bootstrapping should have finished") + } else if vtx0.Status() != choices.Accepted { + t.Fatalf("Vertex should be accepted") + } else if vtx1.Status() != choices.Accepted { + t.Fatalf("Vertex should be accepted") + } +} diff --git a/snow/engine/avalanche/issuer.go b/snow/engine/avalanche/issuer.go index 4cbc408..1807fa1 100644 --- a/snow/engine/avalanche/issuer.go +++ b/snow/engine/avalanche/issuer.go @@ -78,9 +78,12 @@ func (i *issuer) Update() { vdrSet.Add(vdr.ID()) } + toSample := ids.ShortSet{} // Copy to a new variable because we may remove an element in sender.Sender + toSample.Union(vdrSet) // and we don't want that to affect the set of validators we wait for [ie vdrSet] + i.t.RequestID++ - if numVdrs := len(vdrs); numVdrs == p.K && i.t.polls.Add(i.t.RequestID, vdrSet.Len()) { - i.t.Config.Sender.PushQuery(vdrSet, i.t.RequestID, vtxID, i.vtx.Bytes()) + if numVdrs := len(vdrs); numVdrs == p.K && i.t.polls.Add(i.t.RequestID, vdrSet) { + i.t.Config.Sender.PushQuery(toSample, i.t.RequestID, vtxID, i.vtx.Bytes()) } else if numVdrs < p.K { i.t.Config.Context.Log.Error("Query for %s was dropped due to an insufficient number of validators", vtxID) } diff --git a/snow/engine/avalanche/metrics.go b/snow/engine/avalanche/metrics.go index 021fe38..2cd3671 100644 --- a/snow/engine/avalanche/metrics.go +++ b/snow/engine/avalanche/metrics.go @@ -14,7 +14,7 @@ type metrics struct { numBSVtx, numBSDroppedVtx, numBSTx, numBSDroppedTx prometheus.Counter - numPolls, numVtxRequests, numTxRequests, numPendingVtx prometheus.Gauge + numVtxRequests, numTxRequests, numPendingVtx prometheus.Gauge } // Initialize implements the Engine interface @@ -61,12 +61,6 @@ func (m *metrics) Initialize(log logging.Logger, namespace string, registerer pr Name: "av_bs_dropped_txs", Help: "Number of dropped txs", }) - m.numPolls = prometheus.NewGauge( - prometheus.GaugeOpts{ - Namespace: namespace, - Name: "av_polls", - Help: "Number of pending network polls", - }) m.numVtxRequests = prometheus.NewGauge( prometheus.GaugeOpts{ Namespace: namespace, @@ -107,9 +101,6 @@ func (m *metrics) Initialize(log logging.Logger, namespace string, registerer pr if err := registerer.Register(m.numBSDroppedTx); err != nil { log.Error("Failed to register av_bs_dropped_txs statistics due to %s", err) } - if err := registerer.Register(m.numPolls); err != nil { - log.Error("Failed to register av_polls statistics due to %s", err) - } if err := registerer.Register(m.numVtxRequests); err != nil { log.Error("Failed to register av_vtx_requests statistics due to %s", err) } diff --git a/snow/engine/avalanche/poll/early_term_no_traversal.go b/snow/engine/avalanche/poll/early_term_no_traversal.go new file mode 100644 index 0000000..52fdae3 --- /dev/null +++ b/snow/engine/avalanche/poll/early_term_no_traversal.go @@ -0,0 +1,85 @@ +// (c) 2019-2020, Ava Labs, Inc. All rights reserved. +// See the file LICENSE for licensing terms. + +package poll + +import ( + "fmt" + + "github.com/ava-labs/gecko/ids" +) + +type earlyTermNoTraversalFactory struct { + alpha int +} + +// NewEarlyTermNoTraversalFactory returns a factory that returns polls with +// early termination, without doing DAG traversals +func NewEarlyTermNoTraversalFactory(alpha int) Factory { + return &earlyTermNoTraversalFactory{alpha: alpha} +} + +func (f *earlyTermNoTraversalFactory) New(vdrs ids.ShortSet) Poll { + return &earlyTermNoTraversalPoll{ + polled: vdrs, + alpha: f.alpha, + } +} + +// earlyTermNoTraversalPoll finishes when any remaining validators can't change +// the result of the poll. However, does not terminate tightly with this bound. +// It terminates as quickly as it can without performing any DAG traversals. +type earlyTermNoTraversalPoll struct { + votes ids.UniqueBag + polled ids.ShortSet + alpha int +} + +// Vote registers a response for this poll +func (p *earlyTermNoTraversalPoll) Vote(vdr ids.ShortID, votes []ids.ID) { + if !p.polled.Contains(vdr) { + // if the validator wasn't polled or already responded to this poll, we + // should just drop the vote + return + } + + // make sure that a validator can't respond multiple times + p.polled.Remove(vdr) + + // track the votes the validator responded with + p.votes.Add(uint(p.polled.Len()), votes...) +} + +// Finished returns true when all validators have voted +func (p *earlyTermNoTraversalPoll) Finished() bool { + // If there are no outstanding queries, the poll is finished + numPending := p.polled.Len() + if numPending == 0 { + return true + } + // If there are still enough pending responses to include another vertex, + // then the poll must wait for more responses + if numPending > p.alpha { + return false + } + + // Ignore any vertex that has already received alpha votes. To safely skip + // DAG traversal, assume that all votes for vertices with less than alpha + // votes will be applied to a single shared ancestor. In this case, the poll + // can terminate early, iff there are not enough pending votes for this + // ancestor to receive alpha votes. + partialVotes := ids.BitSet(0) + for _, vote := range p.votes.List() { + if voters := p.votes.GetSet(vote); voters.Len() < p.alpha { + partialVotes.Union(voters) + } + } + return partialVotes.Len()+numPending < p.alpha +} + +// Result returns the result of this poll +func (p *earlyTermNoTraversalPoll) Result() ids.UniqueBag { return p.votes } + +func (p *earlyTermNoTraversalPoll) String() string { + return fmt.Sprintf("waiting on %s", p.polled) +} diff --git a/snow/engine/avalanche/poll/early_term_no_traversal_test.go b/snow/engine/avalanche/poll/early_term_no_traversal_test.go new file mode 100644 index 0000000..ba2d81a --- /dev/null +++ b/snow/engine/avalanche/poll/early_term_no_traversal_test.go @@ -0,0 +1,207 @@ +// (c) 2019-2020, Ava Labs, Inc. All rights reserved. +// See the file LICENSE for licensing terms. + +package poll + +import ( + "testing" + + "github.com/ava-labs/gecko/ids" +) + +func TestEarlyTermNoTraversalResults(t *testing.T) { + alpha := 1 + + vtxID := ids.NewID([32]byte{1}) + votes := []ids.ID{vtxID} + + vdr1 := ids.NewShortID([20]byte{1}) // k = 1 + + vdrs := ids.ShortSet{} + vdrs.Add(vdr1) + + factory := NewEarlyTermNoTraversalFactory(alpha) + poll := factory.New(vdrs) + + poll.Vote(vdr1, votes) + if !poll.Finished() { + t.Fatalf("Poll did not terminate after receiving k votes") + } + + result := poll.Result() + if list := result.List(); len(list) != 1 { + t.Fatalf("Wrong number of vertices returned") + } else if retVtxID := list[0]; !retVtxID.Equals(vtxID) { + t.Fatalf("Wrong vertex returned") + } else if set := result.GetSet(vtxID); set.Len() != 1 { + t.Fatalf("Wrong number of votes returned") + } +} + +func TestEarlyTermNoTraversalString(t *testing.T) { + alpha := 2 + + vtxID := ids.NewID([32]byte{1}) + votes := []ids.ID{vtxID} + + vdr1 := ids.NewShortID([20]byte{1}) + vdr2 := ids.NewShortID([20]byte{2}) // k = 2 + + vdrs := ids.ShortSet{} + vdrs.Add( + vdr1, + vdr2, + ) + + factory := NewEarlyTermNoTraversalFactory(alpha) + poll := factory.New(vdrs) + + poll.Vote(vdr1, votes) + + expected := "waiting on {BaMPFdqMUQ46BV8iRcwbVfsam55kMqcp}" + if result := poll.String(); expected != result { + t.Fatalf("Poll should have returned %s but returned %s", expected, result) + } +} + +func TestEarlyTermNoTraversalDropsDuplicatedVotes(t *testing.T) { + alpha := 2 + + vtxID := ids.NewID([32]byte{1}) + votes := []ids.ID{vtxID} + + vdr1 := ids.NewShortID([20]byte{1}) + vdr2 := ids.NewShortID([20]byte{2}) // k = 2 + + vdrs := ids.ShortSet{} + vdrs.Add( + vdr1, + vdr2, + ) + + factory := NewEarlyTermNoTraversalFactory(alpha) + poll := factory.New(vdrs) + + poll.Vote(vdr1, votes) + if poll.Finished() { + t.Fatalf("Poll finished after less than alpha votes") + } + poll.Vote(vdr1, votes) + if poll.Finished() { + t.Fatalf("Poll finished after getting a duplicated vote") + } + poll.Vote(vdr2, votes) + if !poll.Finished() { + t.Fatalf("Poll did not terminate after receiving k votes") + } +} + +func TestEarlyTermNoTraversalTerminatesEarly(t *testing.T) { + alpha := 3 + + vtxID := ids.NewID([32]byte{1}) + votes := []ids.ID{vtxID} + + vdr1 := ids.NewShortID([20]byte{1}) + vdr2 := ids.NewShortID([20]byte{2}) + vdr3 := ids.NewShortID([20]byte{3}) + vdr4 := ids.NewShortID([20]byte{4}) + vdr5 := ids.NewShortID([20]byte{5}) // k = 5 + + vdrs := ids.ShortSet{} + vdrs.Add( + vdr1, + vdr2, + vdr3, + vdr4, + vdr5, + ) + + factory := NewEarlyTermNoTraversalFactory(alpha) + poll := factory.New(vdrs) + + poll.Vote(vdr1, votes) + if poll.Finished() { + t.Fatalf("Poll finished after less than alpha votes") + } + poll.Vote(vdr2, votes) + if poll.Finished() { + t.Fatalf("Poll finished after less than alpha votes") + } + poll.Vote(vdr3, votes) + if !poll.Finished() { + t.Fatalf("Poll did not terminate early after receiving alpha votes for one vertex and none for other vertices") + } +} + +func TestEarlyTermNoTraversalForSharedAncestor(t *testing.T) { + alpha := 4 + + vtxA := ids.NewID([32]byte{1}) + vtxB := ids.NewID([32]byte{2}) + vtxC := ids.NewID([32]byte{3}) + vtxD := ids.NewID([32]byte{4}) + + // If validators 1-3 vote for frontier vertices + // B, C, and D respectively, which all share the common ancestor + // A, then we cannot terminate early with alpha = k = 4 + // If the final vote is cast for any of A, B, C, or D, then + // vertex A will have transitively received alpha = 4 votes + vdr1 := ids.NewShortID([20]byte{1}) + vdr2 := ids.NewShortID([20]byte{2}) + vdr3 := ids.NewShortID([20]byte{3}) + vdr4 := ids.NewShortID([20]byte{4}) + + vdrs := ids.ShortSet{} + vdrs.Add(vdr1) + vdrs.Add(vdr2) + vdrs.Add(vdr3) + vdrs.Add(vdr4) + + factory := NewEarlyTermNoTraversalFactory(alpha) + poll := factory.New(vdrs) + + poll.Vote(vdr1, []ids.ID{vtxB}) + if poll.Finished() { + t.Fatalf("Poll finished early after receiving one vote") + } + poll.Vote(vdr2, []ids.ID{vtxC}) + if poll.Finished() { + t.Fatalf("Poll finished early after receiving two votes") + } + poll.Vote(vdr3, []ids.ID{vtxD}) + if poll.Finished() { + t.Fatalf("Poll terminated early, when a shared ancestor could have received alpha votes") + } + poll.Vote(vdr4, []ids.ID{vtxA}) + if !poll.Finished() { + t.Fatalf("Poll did not terminate after receiving all outstanding votes") + } +} + +func TestEarlyTermNoTraversalWithFastDrops(t *testing.T) { + alpha := 2 + + vdr1 := ids.NewShortID([20]byte{1}) + vdr2 := ids.NewShortID([20]byte{2}) + vdr3 := ids.NewShortID([20]byte{3}) // k = 3 + + vdrs := ids.ShortSet{} + vdrs.Add( + vdr1, + vdr2, + vdr3, + ) + + factory := NewEarlyTermNoTraversalFactory(alpha) + poll := factory.New(vdrs) + + poll.Vote(vdr1, nil) + if poll.Finished() { + t.Fatalf("Poll finished early after dropping one vote") + } + poll.Vote(vdr2, nil) + if !poll.Finished() { + t.Fatalf("Poll did not terminate after dropping two votes") + } +} diff --git a/snow/engine/avalanche/poll/interfaces.go b/snow/engine/avalanche/poll/interfaces.go new file mode 100644 index 0000000..05234a3 --- /dev/null +++ b/snow/engine/avalanche/poll/interfaces.go @@ -0,0 +1,33 @@ +// (c) 2019-2020, Ava Labs, Inc. All rights reserved. +// See the file LICENSE for licensing terms. + +package poll + +import ( + "fmt" + + "github.com/ava-labs/gecko/ids" +) + +// Set is a collection of polls +type Set interface { + fmt.Stringer + + Add(requestID uint32, vdrs ids.ShortSet) bool + Vote(requestID uint32, vdr ids.ShortID, votes []ids.ID) (ids.UniqueBag, bool) + Len() int +} + +// Poll is an outstanding poll +type Poll interface { + fmt.Stringer + + Vote(vdr ids.ShortID, votes []ids.ID) + Finished() bool + Result() ids.UniqueBag +} + +// Factory creates a new Poll +type Factory interface { + New(vdrs ids.ShortSet) Poll +} diff --git a/snow/engine/avalanche/poll/no_early_term.go b/snow/engine/avalanche/poll/no_early_term.go new file mode 100644 index 0000000..9a06649 --- /dev/null +++ b/snow/engine/avalanche/poll/no_early_term.go @@ -0,0 +1,52 @@ +// (c) 2019-2020, Ava Labs, Inc. All rights reserved. +// See the file LICENSE for licensing terms. + +package poll + +import ( + "fmt" + + "github.com/ava-labs/gecko/ids" +) + +type noEarlyTermFactory struct{} + +// NewNoEarlyTermFactory returns a factory that returns polls with no early +// termination +func NewNoEarlyTermFactory() Factory { return noEarlyTermFactory{} } + +func (noEarlyTermFactory) New(vdrs ids.ShortSet) Poll { + return &noEarlyTermPoll{polled: vdrs} +} + +// noEarlyTermPoll finishes when all polled validators either respond to the +// query or a timeout occurs +type noEarlyTermPoll struct { + votes ids.UniqueBag + polled ids.ShortSet +} + +// Vote registers a response for this poll +func (p *noEarlyTermPoll) Vote(vdr ids.ShortID, votes []ids.ID) { + if !p.polled.Contains(vdr) { + // if the validator wasn't polled or already responded to this poll, we + // should just drop the vote + return + } + + // make sure that a validator can't respond multiple times + p.polled.Remove(vdr) + + // track the votes the validator responded with + p.votes.Add(uint(p.polled.Len()), votes...) +} + +// Finished returns true when all validators have voted +func (p *noEarlyTermPoll) Finished() bool { return p.polled.Len() == 0 } + +// Result returns the result of this poll +func (p *noEarlyTermPoll) Result() ids.UniqueBag { return p.votes } + +func (p *noEarlyTermPoll) String() string { + return fmt.Sprintf("waiting on %s", p.polled) +} diff --git a/snow/engine/avalanche/poll/no_early_term_test.go b/snow/engine/avalanche/poll/no_early_term_test.go new file mode 100644 index 0000000..f877416 --- /dev/null +++ b/snow/engine/avalanche/poll/no_early_term_test.go @@ -0,0 +1,91 @@ +// (c) 2019-2020, Ava Labs, Inc. All rights reserved. +// See the file LICENSE for licensing terms. + +package poll + +import ( + "testing" + + "github.com/ava-labs/gecko/ids" +) + +func TestNoEarlyTermResults(t *testing.T) { + vtxID := ids.NewID([32]byte{1}) + votes := []ids.ID{vtxID} + + vdr1 := ids.NewShortID([20]byte{1}) // k = 1 + + vdrs := ids.ShortSet{} + vdrs.Add(vdr1) + + factory := NewNoEarlyTermFactory() + poll := factory.New(vdrs) + + poll.Vote(vdr1, votes) + if !poll.Finished() { + t.Fatalf("Poll did not terminate after receiving k votes") + } + + result := poll.Result() + if list := result.List(); len(list) != 1 { + t.Fatalf("Wrong number of vertices returned") + } else if retVtxID := list[0]; !retVtxID.Equals(vtxID) { + t.Fatalf("Wrong vertex returned") + } else if set := result.GetSet(vtxID); set.Len() != 1 { + t.Fatalf("Wrong number of votes returned") + } +} + +func TestNoEarlyTermString(t *testing.T) { + vtxID := ids.NewID([32]byte{1}) + votes := []ids.ID{vtxID} + + vdr1 := ids.NewShortID([20]byte{1}) + vdr2 := ids.NewShortID([20]byte{2}) // k = 2 + + vdrs := ids.ShortSet{} + vdrs.Add( + vdr1, + vdr2, + ) + + factory := NewNoEarlyTermFactory() + poll := factory.New(vdrs) + + poll.Vote(vdr1, votes) + + expected := "waiting on {BaMPFdqMUQ46BV8iRcwbVfsam55kMqcp}" + if result := poll.String(); expected != result { + t.Fatalf("Poll should have returned %s but returned %s", expected, result) + } +} + +func TestNoEarlyTermDropsDuplicatedVotes(t *testing.T) { + vtxID := ids.NewID([32]byte{1}) + votes := []ids.ID{vtxID} + + vdr1 := ids.NewShortID([20]byte{1}) + vdr2 := ids.NewShortID([20]byte{2}) // k = 2 + + vdrs := ids.ShortSet{} + vdrs.Add( + vdr1, + vdr2, + ) + + factory := NewNoEarlyTermFactory() + poll := factory.New(vdrs) + + poll.Vote(vdr1, votes) + if poll.Finished() { + t.Fatalf("Poll finished after less than alpha votes") + } + poll.Vote(vdr1, votes) + if poll.Finished() { + t.Fatalf("Poll finished after getting a duplicated vote") + } + poll.Vote(vdr2, votes) + if !poll.Finished() { + t.Fatalf("Poll did not terminate after receiving k votes") + } +} diff --git a/snow/engine/avalanche/poll/set.go b/snow/engine/avalanche/poll/set.go new file mode 100644 index 0000000..24c93fb --- /dev/null +++ b/snow/engine/avalanche/poll/set.go @@ -0,0 +1,130 @@ +// (c) 2019-2020, Ava Labs, Inc. All rights reserved. +// See the file LICENSE for licensing terms. + +package poll + +import ( + "fmt" + "strings" + "time" + + "github.com/prometheus/client_golang/prometheus" + + "github.com/ava-labs/gecko/ids" + "github.com/ava-labs/gecko/utils/logging" + "github.com/ava-labs/gecko/utils/timer" +) + +type poll struct { + Poll + start time.Time +} + +type set struct { + log logging.Logger + numPolls prometheus.Gauge + durPolls prometheus.Histogram + factory Factory + polls map[uint32]poll +} + +// NewSet returns a new empty set of polls +func NewSet( + factory Factory, + log logging.Logger, + namespace string, + registerer prometheus.Registerer, +) Set { + numPolls := prometheus.NewGauge(prometheus.GaugeOpts{ + Namespace: namespace, + Name: "polls", + Help: "Number of pending network polls", + }) + if err := registerer.Register(numPolls); err != nil { + log.Error("failed to register polls statistics due to %s", err) + } + + durPolls := prometheus.NewHistogram(prometheus.HistogramOpts{ + Namespace: namespace, + Name: "poll_duration", + Help: "Length of time the poll existed in milliseconds", + Buckets: timer.MillisecondsBuckets, + }) + if err := registerer.Register(durPolls); err != nil { + log.Error("failed to register poll_duration statistics due to %s", err) + } + + return &set{ + log: log, + numPolls: numPolls, + durPolls: durPolls, + factory: factory, + polls: make(map[uint32]poll), + } +} + +// Add to the current set of polls +// Returns true if the poll was registered correctly and the network sample +// should be made. +func (s *set) Add(requestID uint32, vdrs ids.ShortSet) bool { + if _, exists := s.polls[requestID]; exists { + s.log.Debug("dropping poll due to duplicated requestID: %d", requestID) + return false + } + + s.log.Verbo("creating poll with requestID %d and validators %s", + requestID, + vdrs) + + s.polls[requestID] = poll{ + Poll: s.factory.New(vdrs), // create the new poll + start: time.Now(), + } + s.numPolls.Inc() // increase the metrics + return true +} + +// Vote registers the connections response to a query for [id]. If there was no +// query, or the response has already be registered, nothing is performed. +func (s *set) Vote( + requestID uint32, + vdr ids.ShortID, + votes []ids.ID, +) (ids.UniqueBag, bool) { + poll, exists := s.polls[requestID] + if !exists { + s.log.Verbo("dropping vote from %s to an unknown poll with requestID: %d", + vdr, + requestID) + return nil, false + } + + s.log.Verbo("processing vote from %s in the poll with requestID: %d with the votes %v", + vdr, + requestID, + votes) + + poll.Vote(vdr, votes) + if !poll.Finished() { + return nil, false + } + + s.log.Verbo("poll with requestID %d finished as %s", requestID, poll) + + delete(s.polls, requestID) // remove the poll from the current set + s.durPolls.Observe(float64(time.Now().Sub(poll.start).Milliseconds())) + s.numPolls.Dec() // decrease the metrics + return poll.Result(), true +} + +// Len returns the number of outstanding polls +func (s *set) Len() int { return len(s.polls) } + +func (s *set) String() string { + sb := strings.Builder{} + sb.WriteString(fmt.Sprintf("current polls: (Size = %d)", len(s.polls))) + for requestID, poll := range s.polls { + sb.WriteString(fmt.Sprintf("\n %d: %s", requestID, poll)) + } + return sb.String() +} diff --git a/snow/engine/avalanche/poll/set_test.go b/snow/engine/avalanche/poll/set_test.go new file mode 100644 index 0000000..0f0e38e --- /dev/null +++ b/snow/engine/avalanche/poll/set_test.go @@ -0,0 +1,97 @@ +// (c) 2019-2020, Ava Labs, Inc. All rights reserved. +// See the file LICENSE for licensing terms. + +package poll + +import ( + "testing" + + "github.com/ava-labs/gecko/ids" + "github.com/ava-labs/gecko/utils/logging" + "github.com/prometheus/client_golang/prometheus" +) + +func TestNewSetErrorOnMetrics(t *testing.T) { + factory := NewNoEarlyTermFactory() + log := logging.NoLog{} + namespace := "" + registerer := prometheus.NewRegistry() + + registerer.Register(prometheus.NewCounter(prometheus.CounterOpts{ + Name: "polls", + })) + registerer.Register(prometheus.NewCounter(prometheus.CounterOpts{ + Name: "poll_duration", + })) + + _ = NewSet(factory, log, namespace, registerer) +} + +func TestCreateAndFinishPoll(t *testing.T) { + factory := NewNoEarlyTermFactory() + log := logging.NoLog{} + namespace := "" + registerer := prometheus.NewRegistry() + s := NewSet(factory, log, namespace, registerer) + + vtxID := ids.NewID([32]byte{1}) + votes := []ids.ID{vtxID} + + vdr1 := ids.NewShortID([20]byte{1}) + vdr2 := ids.NewShortID([20]byte{2}) // k = 2 + + vdrs := ids.ShortSet{} + vdrs.Add( + vdr1, + vdr2, + ) + + if s.Len() != 0 { + t.Fatalf("Shouldn't have any active polls yet") + } else if !s.Add(0, vdrs) { + t.Fatalf("Should have been able to add a new poll") + } else if s.Len() != 1 { + t.Fatalf("Should only have one active poll") + } else if s.Add(0, vdrs) { + t.Fatalf("Shouldn't have been able to add a duplicated poll") + } else if s.Len() != 1 { + t.Fatalf("Should only have one active poll") + } else if _, finished := s.Vote(1, vdr1, votes); finished { + t.Fatalf("Shouldn't have been able to finish a non-existant poll") + } else if _, finished := s.Vote(0, vdr1, votes); finished { + t.Fatalf("Shouldn't have been able to finish an ongoing poll") + } else if _, finished := s.Vote(0, vdr1, votes); finished { + t.Fatalf("Should have dropped a duplicated poll") + } else if result, finished := s.Vote(0, vdr2, votes); !finished { + t.Fatalf("Should have finished the") + } else if list := result.List(); len(list) != 1 { + t.Fatalf("Wrong number of vertices returned") + } else if retVtxID := list[0]; !retVtxID.Equals(vtxID) { + t.Fatalf("Wrong vertex returned") + } else if set := result.GetSet(vtxID); set.Len() != 2 { + t.Fatalf("Wrong number of votes returned") + } +} + +func TestSetString(t *testing.T) { + factory := NewNoEarlyTermFactory() + log := logging.NoLog{} + namespace := "" + registerer := prometheus.NewRegistry() + s := NewSet(factory, log, namespace, registerer) + + vdr1 := ids.NewShortID([20]byte{1}) // k = 1 + + vdrs := ids.ShortSet{} + vdrs.Add(vdr1) + + expected := "current polls: (Size = 1)\n" + + " 0: waiting on {6HgC8KRBEhXYbF4riJyJFLSHt37UNuRt}" + if !s.Add(0, vdrs) { + t.Fatalf("Should have been able to add a new poll") + } else if str := s.String(); expected != str { + t.Fatalf("Set return wrong string, Expected:\n%s\nReturned:\n%s", + expected, + str) + } +} diff --git a/snow/engine/avalanche/polls.go b/snow/engine/avalanche/polls.go deleted file mode 100644 index 282fe6a..0000000 --- a/snow/engine/avalanche/polls.go +++ /dev/null @@ -1,101 +0,0 @@ -// (c) 2019-2020, Ava Labs, Inc. All rights reserved. -// See the file LICENSE for licensing terms. - -package avalanche - -import ( - "fmt" - "strings" - - "github.com/prometheus/client_golang/prometheus" - - "github.com/ava-labs/gecko/ids" - "github.com/ava-labs/gecko/utils/logging" -) - -// TODO: There is a conservative early termination case that doesn't require dag -// traversals we may want to implement. The algorithm would go as follows: -// Keep track of the number of response that reference an ID. If an ID gets >= -// alpha responses, then remove it from all responses and place it into a chit -// list. Remove all empty responses. If the number of responses + the number of -// pending responses is less than alpha, terminate the poll. -// In the synchronous + virtuous case, when everyone returns the same hash, the -// poll now terminates after receiving alpha responses. -// In the rogue case, it is possible that the poll doesn't terminate as quickly -// as possible, because IDs may have the alpha threshold but only when counting -// transitive votes. In this case, we may wait even if it is no longer possible -// for another ID to earn alpha votes. -// Because alpha is typically set close to k, this may not be performance -// critical. However, early termination may be performance critical with crashed -// nodes. - -type polls struct { - log logging.Logger - numPolls prometheus.Gauge - m map[uint32]poll -} - -// Add to the current set of polls -// Returns true if the poll was registered correctly and the network sample -// should be made. -func (p *polls) Add(requestID uint32, numPolled int) bool { - poll, exists := p.m[requestID] - if !exists { - poll.numPending = numPolled - p.m[requestID] = poll - - p.numPolls.Set(float64(len(p.m))) // Tracks performance statistics - } - return !exists -} - -// Vote registers the connections response to a query for [id]. If there was no -// query, or the response has already be registered, nothing is performed. -func (p *polls) Vote(requestID uint32, vdr ids.ShortID, votes []ids.ID) (ids.UniqueBag, bool) { - p.log.Verbo("Vote. requestID: %d. validatorID: %s.", requestID, vdr) - poll, exists := p.m[requestID] - p.log.Verbo("Poll: %+v", poll) - if !exists { - return nil, false - } - - poll.Vote(votes) - if poll.Finished() { - p.log.Verbo("Poll is finished") - delete(p.m, requestID) - p.numPolls.Set(float64(len(p.m))) // Tracks performance statistics - return poll.votes, true - } - p.m[requestID] = poll - return nil, false -} - -func (p *polls) String() string { - sb := strings.Builder{} - - sb.WriteString(fmt.Sprintf("Current polls: (Size = %d)", len(p.m))) - for requestID, poll := range p.m { - sb.WriteString(fmt.Sprintf("\n %d: %s", requestID, poll)) - } - - return sb.String() -} - -// poll represents the current state of a network poll for a vertex -type poll struct { - votes ids.UniqueBag - numPending int -} - -// Vote registers a vote for this poll -func (p *poll) Vote(votes []ids.ID) { - if p.numPending > 0 { - p.numPending-- - p.votes.Add(uint(p.numPending), votes...) - } -} - -// Finished returns true if the poll has completed, with no more required -// responses -func (p poll) Finished() bool { return p.numPending <= 0 } -func (p poll) String() string { return fmt.Sprintf("Waiting on %d chits", p.numPending) } diff --git a/snow/engine/avalanche/state/unique_vertex.go b/snow/engine/avalanche/state/unique_vertex.go index f7c6927..75c5893 100644 --- a/snow/engine/avalanche/state/unique_vertex.go +++ b/snow/engine/avalanche/state/unique_vertex.go @@ -54,6 +54,8 @@ func (vtx *uniqueVertex) refresh() { func (vtx *uniqueVertex) Evict() { if vtx.v != nil { vtx.v.unique = false + // make sure the parents are able to be garbage collected + vtx.v.parents = nil } } diff --git a/snow/engine/avalanche/transitive.go b/snow/engine/avalanche/transitive.go index e48b167..de714c7 100644 --- a/snow/engine/avalanche/transitive.go +++ b/snow/engine/avalanche/transitive.go @@ -12,6 +12,7 @@ import ( "github.com/ava-labs/gecko/snow/choices" "github.com/ava-labs/gecko/snow/consensus/avalanche" "github.com/ava-labs/gecko/snow/consensus/snowstorm" + "github.com/ava-labs/gecko/snow/engine/avalanche/poll" "github.com/ava-labs/gecko/snow/engine/common" "github.com/ava-labs/gecko/snow/events" "github.com/ava-labs/gecko/utils/formatting" @@ -31,7 +32,7 @@ type Transitive struct { Config bootstrapper - polls polls // track people I have asked for their preference + polls poll.Set // track people I have asked for their preference // vtxReqs prevents asking validators for the same vertex vtxReqs common.Requests @@ -57,9 +58,12 @@ func (t *Transitive) Initialize(config Config) error { t.onFinished = t.finishBootstrapping - t.polls.log = config.Context.Log - t.polls.numPolls = t.numPolls - t.polls.m = make(map[uint32]poll) + factory := poll.NewEarlyTermNoTraversalFactory(int(config.Params.Alpha)) + t.polls = poll.NewSet(factory, + config.Context.Log, + config.Params.Namespace, + config.Params.Metrics, + ) return t.bootstrapper.Initialize(config.BootstrapConfig) } @@ -169,7 +173,11 @@ func (t *Transitive) Put(vdr ids.ShortID, requestID uint32, vtxID ids.ID, vtxByt t.Config.Context.Log.Verbo("Put(%s, %d, %s) called", vdr, requestID, vtxID) if !t.bootstrapped { // Bootstrapping unfinished --> didn't call Get --> this message is invalid - t.Config.Context.Log.Debug("dropping Put(%s, %d, %s) due to bootstrapping", vdr, requestID, vtxID) + if requestID == network.GossipMsgRequestID { + t.Config.Context.Log.Verbo("dropping gossip Put(%s, %d, %s) due to bootstrapping", vdr, requestID, vtxID) + } else { + t.Config.Context.Log.Debug("dropping Put(%s, %d, %s) due to bootstrapping", vdr, requestID, vtxID) + } return nil } @@ -307,7 +315,7 @@ func (t *Transitive) Notify(msg common.Message) error { } func (t *Transitive) repoll() error { - if len(t.polls.m) >= t.Params.ConcurrentRepolls || t.errs.Errored() { + if t.polls.Len() >= t.Params.ConcurrentRepolls || t.errs.Errored() { return nil } @@ -316,7 +324,7 @@ func (t *Transitive) repoll() error { return err } - for i := len(t.polls.m); i < t.Params.ConcurrentRepolls; i++ { + for i := t.polls.Len(); i < t.Params.ConcurrentRepolls; i++ { if err := t.batch(nil, false /*=force*/, true /*=empty*/); err != nil { return err } @@ -335,10 +343,10 @@ func (t *Transitive) reinsertFrom(vdr ids.ShortID, vtxID ids.ID) (bool, error) { func (t *Transitive) insertFrom(vdr ids.ShortID, vtx avalanche.Vertex) (bool, error) { issued := true - vts := []avalanche.Vertex{vtx} - for len(vts) > 0 { - vtx := vts[0] - vts = vts[1:] + vertexHeap := newMaxVertexHeap() + vertexHeap.Push(vtx) + for vertexHeap.Len() > 0 { + vtx := vertexHeap.Pop() if t.Consensus.VertexIssued(vtx) { continue @@ -353,7 +361,7 @@ func (t *Transitive) insertFrom(vdr ids.ShortID, vtx avalanche.Vertex) (bool, er t.sendRequest(vdr, parent.ID()) issued = false } else { - vts = append(vts, parent) + vertexHeap.Push(parent) } } @@ -471,8 +479,11 @@ func (t *Transitive) issueRepoll() { vdrSet.Add(vdr.ID()) } + vdrCopy := ids.ShortSet{} + vdrCopy.Union((vdrSet)) + t.RequestID++ - if numVdrs := len(vdrs); numVdrs == p.K && t.polls.Add(t.RequestID, vdrSet.Len()) { + if numVdrs := len(vdrs); numVdrs == p.K && t.polls.Add(t.RequestID, vdrCopy) { t.Config.Sender.PullQuery(vdrSet, t.RequestID, vtxID) } else if numVdrs < p.K { t.Config.Context.Log.Error("re-query for %s was dropped due to an insufficient number of validators", vtxID) @@ -510,3 +521,8 @@ func (t *Transitive) sendRequest(vdr ids.ShortID, vtxID ids.ID) { t.numVtxRequests.Set(float64(t.vtxReqs.Len())) // Tracks performance statistics } + +// IsBootstrapped returns true iff this chain is done bootstrapping +func (t *Transitive) IsBootstrapped() bool { + return t.bootstrapped +} diff --git a/snow/engine/avalanche/transitive_test.go b/snow/engine/avalanche/transitive_test.go index cc078ca..96b904c 100644 --- a/snow/engine/avalanche/transitive_test.go +++ b/snow/engine/avalanche/transitive_test.go @@ -3085,3 +3085,120 @@ func TestEngineDuplicatedIssuance(t *testing.T) { te.Notify(common.PendingTxs) } + +func TestEngineDoubleChit(t *testing.T) { + config := DefaultConfig() + + config.Params.Alpha = 2 + config.Params.K = 2 + + vdr0 := validators.GenerateRandomValidator(1) + vdr1 := validators.GenerateRandomValidator(1) + vals := validators.NewSet() + vals.Add(vdr0) + vals.Add(vdr1) + config.Validators = vals + + sender := &common.SenderTest{} + sender.T = t + config.Sender = sender + + sender.Default(true) + sender.CantGetAcceptedFrontier = false + + st := &stateTest{t: t} + config.State = st + + st.Default(true) + + gVtx := &Vtx{ + id: GenerateID(), + status: choices.Accepted, + } + mVtx := &Vtx{ + id: GenerateID(), + status: choices.Accepted, + } + + vts := []avalanche.Vertex{gVtx, mVtx} + utxos := []ids.ID{GenerateID()} + + tx := &TestTx{ + TestTx: snowstorm.TestTx{ + Identifier: GenerateID(), + Stat: choices.Processing, + }, + } + tx.Ins.Add(utxos[0]) + + vtx := &Vtx{ + parents: vts, + id: GenerateID(), + txs: []snowstorm.Tx{tx}, + height: 1, + status: choices.Processing, + bytes: []byte{1, 1, 2, 3}, + } + + st.edge = func() []ids.ID { return []ids.ID{vts[0].ID(), vts[1].ID()} } + st.getVertex = func(id ids.ID) (avalanche.Vertex, error) { + switch { + case id.Equals(gVtx.ID()): + return gVtx, nil + case id.Equals(mVtx.ID()): + return mVtx, nil + } + t.Fatalf("Unknown vertex") + panic("Should have errored") + } + + te := &Transitive{} + te.Initialize(config) + te.finishBootstrapping() + + reqID := new(uint32) + sender.PushQueryF = func(inVdrs ids.ShortSet, requestID uint32, vtxID ids.ID, _ []byte) { + *reqID = requestID + if inVdrs.Len() != 2 { + t.Fatalf("Wrong number of validators") + } + if !vtxID.Equals(vtx.ID()) { + t.Fatalf("Wrong vertex requested") + } + } + st.getVertex = func(id ids.ID) (avalanche.Vertex, error) { + switch { + case id.Equals(vtx.ID()): + return vtx, nil + } + t.Fatalf("Unknown vertex") + panic("Should have errored") + } + + te.insert(vtx) + + votes := ids.Set{} + votes.Add(vtx.ID()) + + if status := tx.Status(); status != choices.Processing { + t.Fatalf("Wrong tx status: %s ; expected: %s", status, choices.Processing) + } + + te.Chits(vdr0.ID(), *reqID, votes) + + if status := tx.Status(); status != choices.Processing { + t.Fatalf("Wrong tx status: %s ; expected: %s", status, choices.Processing) + } + + te.Chits(vdr0.ID(), *reqID, votes) + + if status := tx.Status(); status != choices.Processing { + t.Fatalf("Wrong tx status: %s ; expected: %s", status, choices.Processing) + } + + te.Chits(vdr1.ID(), *reqID, votes) + + if status := tx.Status(); status != choices.Accepted { + t.Fatalf("Wrong tx status: %s ; expected: %s", status, choices.Accepted) + } +} diff --git a/snow/engine/common/bootstrapper.go b/snow/engine/common/bootstrapper.go index 49f4051..f1f58db 100644 --- a/snow/engine/common/bootstrapper.go +++ b/snow/engine/common/bootstrapper.go @@ -17,11 +17,14 @@ const ( // StatusUpdateFrequency ... bootstrapper logs "processed X blocks/vertices" every [statusUpdateFrequency] blocks/vertices StatusUpdateFrequency = 2500 + + // MaxOutstandingRequests is the maximum number of GetAncestors sent but not responsded to/failed + MaxOutstandingRequests = 8 ) var ( // MaxTimeFetchingAncestors is the maximum amount of time to spend fetching vertices during a call to GetAncestors - MaxTimeFetchingAncestors = 100 * time.Millisecond + MaxTimeFetchingAncestors = 50 * time.Millisecond ) // Bootstrapper implements the Engine interface. diff --git a/snow/engine/common/engine.go b/snow/engine/common/engine.go index 3be916d..7ca5622 100644 --- a/snow/engine/common/engine.go +++ b/snow/engine/common/engine.go @@ -14,6 +14,9 @@ type Engine interface { // Return the context of the chain this engine is working on Context() *snow.Context + + // Returns true iff the chain is done bootstrapping + IsBootstrapped() bool } // Handler defines the functions that are acted on the node diff --git a/snow/engine/common/requests.go b/snow/engine/common/requests.go index 22d5759..51f6cb3 100644 --- a/snow/engine/common/requests.go +++ b/snow/engine/common/requests.go @@ -7,6 +7,10 @@ import ( "github.com/ava-labs/gecko/ids" ) +const ( + minRequestsSize = 32 +) + type req struct { vdr ids.ShortID id uint32 @@ -22,7 +26,7 @@ type Requests struct { // are only in one request at a time. func (r *Requests) Add(vdr ids.ShortID, requestID uint32, containerID ids.ID) { if r.reqsToID == nil { - r.reqsToID = make(map[[20]byte]map[uint32]ids.ID) + r.reqsToID = make(map[[20]byte]map[uint32]ids.ID, minRequestsSize) } vdrKey := vdr.Key() vdrReqs, ok := r.reqsToID[vdrKey] @@ -33,7 +37,7 @@ func (r *Requests) Add(vdr ids.ShortID, requestID uint32, containerID ids.ID) { vdrReqs[requestID] = containerID if r.idToReq == nil { - r.idToReq = make(map[[32]byte]req) + r.idToReq = make(map[[32]byte]req, minRequestsSize) } r.idToReq[containerID.Key()] = req{ vdr: vdr, diff --git a/snow/engine/common/test_engine.go b/snow/engine/common/test_engine.go index cf63df8..2f4c511 100644 --- a/snow/engine/common/test_engine.go +++ b/snow/engine/common/test_engine.go @@ -15,6 +15,7 @@ import ( type EngineTest struct { T *testing.T + CantIsBootstrapped, CantStartup, CantGossip, CantShutdown, @@ -43,6 +44,7 @@ type EngineTest struct { CantQueryFailed, CantChits bool + IsBootstrappedF func() bool ContextF func() *snow.Context StartupF, GossipF, ShutdownF func() error NotifyF func(Message) error @@ -58,6 +60,8 @@ var _ Engine = &EngineTest{} // Default ... func (e *EngineTest) Default(cant bool) { + e.CantIsBootstrapped = cant + e.CantStartup = cant e.CantGossip = cant e.CantShutdown = cant @@ -354,3 +358,14 @@ func (e *EngineTest) Chits(validatorID ids.ShortID, requestID uint32, containerI } return nil } + +// IsBootstrapped ... +func (e *EngineTest) IsBootstrapped() bool { + if e.IsBootstrappedF != nil { + return e.IsBootstrappedF() + } + if e.CantIsBootstrapped && e.T != nil { + e.T.Fatalf("Unexpectedly called IsBootstrapped") + } + return false +} diff --git a/snow/engine/snowman/bootstrapper.go b/snow/engine/snowman/bootstrapper.go index b7df6b8..5ee73bc 100644 --- a/snow/engine/snowman/bootstrapper.go +++ b/snow/engine/snowman/bootstrapper.go @@ -115,6 +115,9 @@ func (b *bootstrapper) fetch(blkID ids.ID) error { // Make sure we don't already have this block if _, err := b.VM.GetBlock(blkID); err == nil { + if numPending := b.outstandingRequests.Len(); numPending == 0 && b.processedStartingAcceptedFrontier { + return b.finish() + } return nil } @@ -224,7 +227,8 @@ func (b *bootstrapper) finish() error { if b.finished { return nil } - b.BootstrapConfig.Context.Log.Info("bootstrapping finished fetching blocks. executing state transitions...") + b.BootstrapConfig.Context.Log.Info("bootstrapping finished fetching %d blocks. executing state transitions...", + b.numFetched) if err := b.executeAll(b.Blocked, b.numBlocked); err != nil { return err @@ -262,5 +266,6 @@ func (b *bootstrapper) executeAll(jobs *queue.Jobs, numBlocked prometheus.Gauge) b.BootstrapConfig.Context.Log.Info("executed %d blocks", numExecuted) } } + b.BootstrapConfig.Context.Log.Info("executed %d blocks", numExecuted) return nil } diff --git a/snow/engine/snowman/bootstrapper_test.go b/snow/engine/snowman/bootstrapper_test.go index 286d9d7..016e05c 100644 --- a/snow/engine/snowman/bootstrapper_test.go +++ b/snow/engine/snowman/bootstrapper_test.go @@ -622,3 +622,128 @@ func TestBootstrapperFilterAccepted(t *testing.T) { t.Fatalf("Blk shouldn't be accepted") } } + +func TestBootstrapperFinalized(t *testing.T) { + config, peerID, sender, vm := newConfig(t) + + blkID0 := ids.Empty.Prefix(0) + blkID1 := ids.Empty.Prefix(1) + blkID2 := ids.Empty.Prefix(2) + + blkBytes0 := []byte{0} + blkBytes1 := []byte{1} + blkBytes2 := []byte{2} + + blk0 := &Blk{ + id: blkID0, + height: 0, + status: choices.Accepted, + bytes: blkBytes0, + } + blk1 := &Blk{ + parent: blk0, + id: blkID1, + height: 1, + status: choices.Unknown, + bytes: blkBytes1, + } + blk2 := &Blk{ + parent: blk1, + id: blkID2, + height: 2, + status: choices.Unknown, + bytes: blkBytes2, + } + + bs := bootstrapper{} + bs.metrics.Initialize(config.Context.Log, fmt.Sprintf("gecko_%s", config.Context.ChainID), prometheus.NewRegistry()) + bs.Initialize(config) + finished := new(bool) + bs.onFinished = func() error { *finished = true; return nil } + + acceptedIDs := ids.Set{} + acceptedIDs.Add(blkID1) + acceptedIDs.Add(blkID2) + + parsedBlk1 := false + parsedBlk2 := false + vm.GetBlockF = func(blkID ids.ID) (snowman.Block, error) { + switch { + case blkID.Equals(blkID0): + return blk0, nil + case blkID.Equals(blkID1): + if parsedBlk1 { + return blk1, nil + } + return nil, errUnknownBlock + case blkID.Equals(blkID2): + if parsedBlk2 { + return blk2, nil + } + return nil, errUnknownBlock + default: + t.Fatal(errUnknownBlock) + panic(errUnknownBlock) + } + } + vm.ParseBlockF = func(blkBytes []byte) (snowman.Block, error) { + switch { + case bytes.Equal(blkBytes, blkBytes0): + return blk0, nil + case bytes.Equal(blkBytes, blkBytes1): + blk1.status = choices.Processing + parsedBlk1 = true + return blk1, nil + case bytes.Equal(blkBytes, blkBytes2): + blk2.status = choices.Processing + parsedBlk2 = true + return blk2, nil + } + t.Fatal(errUnknownBlock) + return nil, errUnknownBlock + } + + requestIDs := map[[32]byte]uint32{} + sender.GetAncestorsF = func(vdr ids.ShortID, reqID uint32, vtxID ids.ID) { + if !vdr.Equals(peerID) { + t.Fatalf("Should have requested block from %s, requested from %s", peerID, vdr) + } + requestIDs[vtxID.Key()] = reqID + } + + vm.CantBootstrapping = false + + if err := bs.ForceAccepted(acceptedIDs); err != nil { // should request blk0 and blk1 + t.Fatal(err) + } + + reqID, ok := requestIDs[blkID2.Key()] + if !ok { + t.Fatalf("should have requested blk2") + } + + vm.CantBootstrapped = false + + if err := bs.MultiPut(peerID, reqID, [][]byte{blkBytes2, blkBytes1}); err != nil { + t.Fatal(err) + } + + reqID, ok = requestIDs[blkID1.Key()] + if !ok { + t.Fatalf("should have requested blk1") + } + + if err := bs.GetAncestorsFailed(peerID, reqID); err != nil { + t.Fatal(err) + } + + if !*finished { + t.Fatalf("Bootstrapping should have finished") + } else if blk0.Status() != choices.Accepted { + t.Fatalf("Block should be accepted") + } else if blk1.Status() != choices.Accepted { + t.Fatalf("Block should be accepted") + } else if blk2.Status() != choices.Accepted { + t.Fatalf("Block should be accepted") + } +} diff --git a/snow/engine/snowman/metrics.go b/snow/engine/snowman/metrics.go index f17d360..d71697a 100644 --- a/snow/engine/snowman/metrics.go +++ b/snow/engine/snowman/metrics.go @@ -13,7 +13,7 @@ type metrics struct { numPendingRequests, numBlocked prometheus.Gauge numBootstrapped, numDropped prometheus.Counter - numPolls, numBlkRequests, numBlockedBlk prometheus.Gauge + numBlkRequests, numBlockedBlk prometheus.Gauge } // Initialize implements the Engine interface @@ -42,12 +42,6 @@ func (m *metrics) Initialize(log logging.Logger, namespace string, registerer pr Name: "sm_bs_dropped", Help: "Number of dropped bootstrap blocks", }) - m.numPolls = prometheus.NewGauge( - prometheus.GaugeOpts{ - Namespace: namespace, - Name: "sm_polls", - Help: "Number of pending network polls", - }) m.numBlkRequests = prometheus.NewGauge( prometheus.GaugeOpts{ Namespace: namespace, @@ -73,9 +67,6 @@ func (m *metrics) Initialize(log logging.Logger, namespace string, registerer pr if err := registerer.Register(m.numDropped); err != nil { log.Error("Failed to register sm_bs_dropped statistics due to %s", err) } - if err := registerer.Register(m.numPolls); err != nil { - log.Error("Failed to register sm_polls statistics due to %s", err) - } if err := registerer.Register(m.numBlkRequests); err != nil { log.Error("Failed to register sm_blk_requests statistics due to %s", err) } diff --git a/snow/engine/snowman/poll/early_term_no_traversal.go b/snow/engine/snowman/poll/early_term_no_traversal.go new file mode 100644 index 0000000..8042b27 --- /dev/null +++ b/snow/engine/snowman/poll/early_term_no_traversal.go @@ -0,0 +1,73 @@ +// (c) 2019-2020, Ava Labs, Inc. All rights reserved. +// See the file LICENSE for licensing terms. + +package poll + +import ( + "fmt" + + "github.com/ava-labs/gecko/ids" +) + +type earlyTermNoTraversalFactory struct { + alpha int +} + +// NewEarlyTermNoTraversalFactory returns a factory that returns polls with +// early termination, without doing DAG traversals +func NewEarlyTermNoTraversalFactory(alpha int) Factory { + return &earlyTermNoTraversalFactory{alpha: alpha} +} + +func (f *earlyTermNoTraversalFactory) New(vdrs ids.ShortSet) Poll { + return &earlyTermNoTraversalPoll{ + polled: vdrs, + alpha: f.alpha, + } +} + +// earlyTermNoTraversalPoll finishes when any remaining validators can't change +// the result of the poll. However, does not terminate tightly with this bound. +// It terminates as quickly as it can without performing any DAG traversals. +type earlyTermNoTraversalPoll struct { + votes ids.Bag + polled ids.ShortSet + alpha int +} + +// Vote registers a response for this poll +func (p *earlyTermNoTraversalPoll) Vote(vdr ids.ShortID, vote ids.ID) { + if !p.polled.Contains(vdr) { + // if the validator wasn't polled or already responded to this poll, we + // should just drop the vote + return + } + + // make sure that a validator can't respond multiple times + p.polled.Remove(vdr) + + // track the votes the validator responded with + p.votes.Add(vote) +} + +// Drop any future response for this poll +func (p *earlyTermNoTraversalPoll) Drop(vdr ids.ShortID) { + p.polled.Remove(vdr) +} + +// Finished returns true when all validators have voted +func (p *earlyTermNoTraversalPoll) Finished() bool { + remaining := p.polled.Len() + received := p.votes.Len() + _, freq := p.votes.Mode() + return remaining == 0 || // All k nodes responded + freq >= p.alpha || // An alpha majority has returned + received+remaining < p.alpha // An alpha majority can never return +} + +// Result returns the result of this poll +func (p *earlyTermNoTraversalPoll) Result() ids.Bag { return p.votes } + +func (p *earlyTermNoTraversalPoll) String() string { + return fmt.Sprintf("waiting on %s", p.polled) +} diff --git a/snow/engine/snowman/poll/early_term_no_traversal_test.go b/snow/engine/snowman/poll/early_term_no_traversal_test.go new file mode 100644 index 0000000..dd444e9 --- /dev/null +++ b/snow/engine/snowman/poll/early_term_no_traversal_test.go @@ -0,0 +1,205 @@ +// (c) 2019-2020, Ava Labs, Inc. All rights reserved. +// See the file LICENSE for licensing terms. + +package poll + +import ( + "testing" + + "github.com/ava-labs/gecko/ids" +) + +func TestEarlyTermNoTraversalResults(t *testing.T) { + alpha := 1 + + vtxID := ids.NewID([32]byte{1}) + + vdr1 := ids.NewShortID([20]byte{1}) // k = 1 + + vdrs := ids.ShortSet{} + vdrs.Add(vdr1) + + factory := NewEarlyTermNoTraversalFactory(alpha) + poll := factory.New(vdrs) + + poll.Vote(vdr1, vtxID) + if !poll.Finished() { + t.Fatalf("Poll did not terminate after receiving k votes") + } + + result := poll.Result() + if list := result.List(); len(list) != 1 { + t.Fatalf("Wrong number of vertices returned") + } else if retVtxID := list[0]; !retVtxID.Equals(vtxID) { + t.Fatalf("Wrong vertex returned") + } else if result.Count(vtxID) != 1 { + t.Fatalf("Wrong number of votes returned") + } +} + +func TestEarlyTermNoTraversalString(t *testing.T) { + alpha := 2 + + vtxID := ids.NewID([32]byte{1}) + + vdr1 := ids.NewShortID([20]byte{1}) + vdr2 := ids.NewShortID([20]byte{2}) // k = 2 + + vdrs := ids.ShortSet{} + vdrs.Add( + vdr1, + vdr2, + ) + + factory := NewEarlyTermNoTraversalFactory(alpha) + poll := factory.New(vdrs) + + poll.Vote(vdr1, vtxID) + + expected := "waiting on {BaMPFdqMUQ46BV8iRcwbVfsam55kMqcp}" + if result := poll.String(); expected != result { + t.Fatalf("Poll should have returned %s but returned %s", expected, result) + } +} + +func TestEarlyTermNoTraversalDropsDuplicatedVotes(t *testing.T) { + alpha := 2 + + vtxID := ids.NewID([32]byte{1}) + + vdr1 := ids.NewShortID([20]byte{1}) + vdr2 := ids.NewShortID([20]byte{2}) // k = 2 + + vdrs := ids.ShortSet{} + vdrs.Add( + vdr1, + vdr2, + ) + + factory := NewEarlyTermNoTraversalFactory(alpha) + poll := factory.New(vdrs) + + poll.Vote(vdr1, vtxID) + if poll.Finished() { + t.Fatalf("Poll finished after less than alpha votes") + } + poll.Vote(vdr1, vtxID) + if poll.Finished() { + t.Fatalf("Poll finished after getting a duplicated vote") + } + poll.Vote(vdr2, vtxID) + if !poll.Finished() { + t.Fatalf("Poll did not terminate after receiving k votes") + } +} + +func TestEarlyTermNoTraversalTerminatesEarly(t *testing.T) { + alpha := 3 + + vtxID := ids.NewID([32]byte{1}) + + vdr1 := ids.NewShortID([20]byte{1}) + vdr2 := ids.NewShortID([20]byte{2}) + vdr3 := ids.NewShortID([20]byte{3}) + vdr4 := ids.NewShortID([20]byte{4}) + vdr5 := ids.NewShortID([20]byte{5}) // k = 5 + + vdrs := ids.ShortSet{} + vdrs.Add( + vdr1, + vdr2, + vdr3, + vdr4, + vdr5, + ) + + factory := NewEarlyTermNoTraversalFactory(alpha) + poll := factory.New(vdrs) + + poll.Vote(vdr1, vtxID) + if poll.Finished() { + t.Fatalf("Poll finished after less than alpha votes") + } + poll.Vote(vdr2, vtxID) + if poll.Finished() { + t.Fatalf("Poll finished after less than alpha votes") + } + poll.Vote(vdr3, vtxID) + if !poll.Finished() { + t.Fatalf("Poll did not terminate early after receiving alpha votes for one vertex and none for other vertices") + } +} + +func TestEarlyTermNoTraversalForSharedAncestor(t *testing.T) { + alpha := 4 + + vtxA := ids.NewID([32]byte{1}) + vtxB := ids.NewID([32]byte{2}) + vtxC := ids.NewID([32]byte{3}) + vtxD := ids.NewID([32]byte{4}) + + // If validators 1-3 vote for frontier vertices + // B, C, and D respectively, which all share the common ancestor + // A, then we cannot terminate early with alpha = k = 4 + // If the final vote is cast for any of A, B, C, or D, then + // vertex A will have transitively received alpha = 4 votes + vdr1 := ids.NewShortID([20]byte{1}) + vdr2 := ids.NewShortID([20]byte{2}) + vdr3 := ids.NewShortID([20]byte{3}) + vdr4 := ids.NewShortID([20]byte{4}) + + vdrs := ids.ShortSet{} + vdrs.Add( + vdr1, + vdr2, + vdr3, + vdr4, + ) + + factory := NewEarlyTermNoTraversalFactory(alpha) + poll := factory.New(vdrs) + + poll.Vote(vdr1, vtxB) + if poll.Finished() { + t.Fatalf("Poll finished early after receiving one vote") + } + poll.Vote(vdr2, vtxC) + if poll.Finished() { + t.Fatalf("Poll finished early after receiving two votes") + } + poll.Vote(vdr3, vtxD) + if poll.Finished() { + t.Fatalf("Poll terminated early, when a shared ancestor could have received alpha votes") + } + poll.Vote(vdr4, vtxA) + if !poll.Finished() { + t.Fatalf("Poll did not terminate after receiving all outstanding votes") + } +} + +func TestEarlyTermNoTraversalWithFastDrops(t *testing.T) { + alpha := 2 + + vdr1 := ids.NewShortID([20]byte{1}) + vdr2 := ids.NewShortID([20]byte{2}) + vdr3 := ids.NewShortID([20]byte{3}) // k = 3 + + vdrs := ids.ShortSet{} + vdrs.Add( + vdr1, + vdr2, + vdr3, + ) + + factory := NewEarlyTermNoTraversalFactory(alpha) + poll := factory.New(vdrs) + + poll.Drop(vdr1) + if poll.Finished() { + t.Fatalf("Poll finished early after dropping one vote") + } + poll.Drop(vdr2) + if !poll.Finished() { + t.Fatalf("Poll did not terminate after dropping two votes") + } +} diff --git a/snow/engine/snowman/poll/interfaces.go b/snow/engine/snowman/poll/interfaces.go new file mode 100644 index 0000000..33731ad --- /dev/null +++ b/snow/engine/snowman/poll/interfaces.go @@ -0,0 +1,35 @@ +// (c) 2019-2020, Ava Labs, Inc. All rights reserved. +// See the file LICENSE for licensing terms. + +package poll + +import ( + "fmt" + + "github.com/ava-labs/gecko/ids" +) + +// Set is a collection of polls +type Set interface { + fmt.Stringer + + Add(requestID uint32, vdrs ids.ShortSet) bool + Vote(requestID uint32, vdr ids.ShortID, vote ids.ID) (ids.Bag, bool) + Drop(requestID uint32, vdr ids.ShortID) (ids.Bag, bool) + Len() int +} + +// Poll is an outstanding poll +type Poll interface { + fmt.Stringer + + Vote(vdr ids.ShortID, vote ids.ID) + Drop(vdr ids.ShortID) + Finished() bool + Result() ids.Bag +} + +// Factory creates a new Poll +type Factory interface { + New(vdrs ids.ShortSet) Poll +} diff --git a/snow/engine/snowman/poll/no_early_term.go b/snow/engine/snowman/poll/no_early_term.go new file mode 100644 index 0000000..3bcaf38 --- /dev/null +++ b/snow/engine/snowman/poll/no_early_term.go @@ -0,0 +1,55 @@ +// (c) 2019-2020, Ava Labs, Inc. All rights reserved. +// See the file LICENSE for licensing terms. + +package poll + +import ( + "fmt" + + "github.com/ava-labs/gecko/ids" +) + +type noEarlyTermFactory struct{} + +// NewNoEarlyTermFactory returns a factory that returns polls with no early +// termination +func NewNoEarlyTermFactory() Factory { return noEarlyTermFactory{} } + +func (noEarlyTermFactory) New(vdrs ids.ShortSet) Poll { + return &noEarlyTermPoll{polled: vdrs} +} + +// noEarlyTermPoll finishes when all polled validators either respond to the +// query or a timeout occurs +type noEarlyTermPoll struct { + votes ids.Bag + polled ids.ShortSet +} + +// Vote registers a response for this poll +func (p *noEarlyTermPoll) Vote(vdr ids.ShortID, vote ids.ID) { + if !p.polled.Contains(vdr) { + // if the validator wasn't polled or already responded to this poll, we + // should just drop the vote + return + } + + // make sure that a validator can't respond multiple times + p.polled.Remove(vdr) + + // track the votes the validator responded with + p.votes.Add(vote) +} + +// Drop any future response for this poll +func (p *noEarlyTermPoll) Drop(vdr ids.ShortID) { p.polled.Remove(vdr) } + +// Finished returns true when all validators have voted +func (p *noEarlyTermPoll) Finished() bool { return p.polled.Len() == 0 } + +// Result returns the result of this poll +func (p *noEarlyTermPoll) Result() ids.Bag { return p.votes } + +func (p *noEarlyTermPoll) String() string { + return fmt.Sprintf("waiting on %s", p.polled) +} diff --git a/snow/engine/snowman/poll/no_early_term_test.go b/snow/engine/snowman/poll/no_early_term_test.go new file mode 100644 index 0000000..a366b5e --- /dev/null +++ b/snow/engine/snowman/poll/no_early_term_test.go @@ -0,0 +1,92 @@ +// (c) 2019-2020, Ava Labs, Inc. All rights reserved. +// See the file LICENSE for licensing terms. + +package poll + +import ( + "testing" + + "github.com/ava-labs/gecko/ids" +) + +func TestNoEarlyTermResults(t *testing.T) { + vtxID := ids.NewID([32]byte{1}) + + vdr1 := ids.NewShortID([20]byte{1}) // k = 1 + + vdrs := ids.ShortSet{} + vdrs.Add(vdr1) + + factory := NewNoEarlyTermFactory() + poll := factory.New(vdrs) + + poll.Vote(vdr1, vtxID) + if !poll.Finished() { + t.Fatalf("Poll did not terminate after receiving k votes") + } + + result := poll.Result() + if list := result.List(); len(list) != 1 { + t.Fatalf("Wrong number of vertices returned") + } else if retVtxID := list[0]; !retVtxID.Equals(vtxID) { + t.Fatalf("Wrong vertex returned") + } else if result.Count(vtxID) != 1 { + t.Fatalf("Wrong number of votes returned") + } +} + +func TestNoEarlyTermString(t *testing.T) { + vtxID := ids.NewID([32]byte{1}) + + vdr1 := ids.NewShortID([20]byte{1}) + vdr2 := ids.NewShortID([20]byte{2}) // k = 2 + + vdrs := ids.ShortSet{} + vdrs.Add( + vdr1, + vdr2, + ) + + factory := NewNoEarlyTermFactory() + poll := factory.New(vdrs) + + poll.Vote(vdr1, vtxID) + + expected := "waiting on {BaMPFdqMUQ46BV8iRcwbVfsam55kMqcp}" + if result := poll.String(); expected != result { + t.Fatalf("Poll should have returned %s but returned %s", expected, result) + } +} + +func TestNoEarlyTermDropsDuplicatedVotes(t *testing.T) { + vtxID := ids.NewID([32]byte{1}) + + vdr1 := ids.NewShortID([20]byte{1}) + vdr2 := ids.NewShortID([20]byte{2}) // k = 2 + + vdrs := ids.ShortSet{} + vdrs.Add( + vdr1, + vdr2, + ) + + factory := NewNoEarlyTermFactory() + poll := factory.New(vdrs) + + poll.Vote(vdr1, vtxID) + if poll.Finished() { + t.Fatalf("Poll finished after less than alpha votes") + } + poll.Vote(vdr1, vtxID) + if poll.Finished() { + t.Fatalf("Poll finished after getting a duplicated vote") + } + poll.Drop(vdr1) + if poll.Finished() { + t.Fatalf("Poll finished after getting a duplicated vote") + } + poll.Vote(vdr2, vtxID) + if !poll.Finished() { + t.Fatalf("Poll did not terminate after receiving k votes") + } +} diff --git a/snow/engine/snowman/poll/set.go b/snow/engine/snowman/poll/set.go new file mode 100644 index 0000000..25e0e68 --- /dev/null +++ b/snow/engine/snowman/poll/set.go @@ -0,0 +1,158 @@ +// (c) 2019-2020, Ava Labs, Inc. All rights reserved. +// See the file LICENSE for licensing terms. + +package poll + +import ( + "fmt" + "strings" + "time" + + "github.com/prometheus/client_golang/prometheus" + + "github.com/ava-labs/gecko/ids" + "github.com/ava-labs/gecko/utils/logging" + "github.com/ava-labs/gecko/utils/timer" +) + +type poll struct { + Poll + start time.Time +} + +type set struct { + log logging.Logger + numPolls prometheus.Gauge + durPolls prometheus.Histogram + factory Factory + polls map[uint32]poll +} + +// NewSet returns a new empty set of polls +func NewSet( + factory Factory, + log logging.Logger, + namespace string, + registerer prometheus.Registerer, +) Set { + numPolls := prometheus.NewGauge(prometheus.GaugeOpts{ + Namespace: namespace, + Name: "polls", + Help: "Number of pending network polls", + }) + if err := registerer.Register(numPolls); err != nil { + log.Error("failed to register polls statistics due to %s", err) + } + + durPolls := prometheus.NewHistogram(prometheus.HistogramOpts{ + Namespace: namespace, + Name: "poll_duration", + Help: "Length of time the poll existed in milliseconds", + Buckets: timer.MillisecondsBuckets, + }) + if err := registerer.Register(durPolls); err != nil { + log.Error("failed to register poll_duration statistics due to %s", err) + } + + return &set{ + log: log, + numPolls: numPolls, + durPolls: durPolls, + factory: factory, + polls: make(map[uint32]poll), + } +} + +// Add to the current set of polls +// Returns true if the poll was registered correctly and the network sample +// should be made. +func (s *set) Add(requestID uint32, vdrs ids.ShortSet) bool { + if _, exists := s.polls[requestID]; exists { + s.log.Debug("dropping poll due to duplicated requestID: %d", requestID) + return false + } + + s.log.Verbo("creating poll with requestID %d and validators %s", + requestID, + vdrs) + + s.polls[requestID] = poll{ + Poll: s.factory.New(vdrs), // create the new poll + start: time.Now(), + } + s.numPolls.Inc() // increase the metrics + return true +} + +// Vote registers the connections response to a query for [id]. If there was no +// query, or the response has already be registered, nothing is performed. +func (s *set) Vote( + requestID uint32, + vdr ids.ShortID, + vote ids.ID, +) (ids.Bag, bool) { + poll, exists := s.polls[requestID] + if !exists { + s.log.Verbo("dropping vote from %s to an unknown poll with requestID: %d", + vdr, + requestID) + return ids.Bag{}, false + } + + s.log.Verbo("processing vote from %s in the poll with requestID: %d with the vote %s", + vdr, + requestID, + vote) + + poll.Vote(vdr, vote) + if !poll.Finished() { + return ids.Bag{}, false + } + + s.log.Verbo("poll with requestID %d finished as %s", requestID, poll) + + delete(s.polls, requestID) // remove the poll from the current set + s.durPolls.Observe(float64(time.Now().Sub(poll.start).Milliseconds())) + s.numPolls.Dec() // decrease the metrics + return poll.Result(), true +} + +// Drop registers the connections response to a query for [id]. If there was no +// query, or the response has already be registered, nothing is performed. +func (s *set) Drop(requestID uint32, vdr ids.ShortID) (ids.Bag, bool) { + poll, exists := s.polls[requestID] + if !exists { + s.log.Verbo("dropping vote from %s to an unknown poll with requestID: %d", + vdr, + requestID) + return ids.Bag{}, false + } + + s.log.Verbo("processing dropped vote from %s in the poll with requestID: %d", + vdr, + requestID) + + poll.Drop(vdr) + if !poll.Finished() { + return ids.Bag{}, false + } + + s.log.Verbo("poll with requestID %d finished as %s", requestID, poll) + + delete(s.polls, requestID) // remove the poll from the current set + s.durPolls.Observe(float64(time.Now().Sub(poll.start).Milliseconds())) + s.numPolls.Dec() // decrease the metrics + return poll.Result(), true +} + +// Len returns the number of outstanding polls +func (s *set) Len() int { return len(s.polls) } + +func (s *set) String() string { + sb := strings.Builder{} + sb.WriteString(fmt.Sprintf("current polls: (Size = %d)", len(s.polls))) + for requestID, poll := range s.polls { + sb.WriteString(fmt.Sprintf("\n %d: %s", requestID, poll)) + } + return sb.String() +} diff --git a/snow/engine/snowman/poll/set_test.go b/snow/engine/snowman/poll/set_test.go new file mode 100644 index 0000000..2ccf0f3 --- /dev/null +++ b/snow/engine/snowman/poll/set_test.go @@ -0,0 +1,135 @@ +// (c) 2019-2020, Ava Labs, Inc. All rights reserved. +// See the file LICENSE for licensing terms. + +package poll + +import ( + "testing" + + "github.com/ava-labs/gecko/ids" + "github.com/ava-labs/gecko/utils/logging" + "github.com/prometheus/client_golang/prometheus" +) + +func TestNewSetErrorOnMetrics(t *testing.T) { + factory := NewNoEarlyTermFactory() + log := logging.NoLog{} + namespace := "" + registerer := prometheus.NewRegistry() + + registerer.Register(prometheus.NewCounter(prometheus.CounterOpts{ + Name: "polls", + })) + registerer.Register(prometheus.NewCounter(prometheus.CounterOpts{ + Name: "poll_duration", + })) + + _ = NewSet(factory, log, namespace, registerer) +} + +func TestCreateAndFinishSuccessfulPoll(t *testing.T) { + factory := NewNoEarlyTermFactory() + log := logging.NoLog{} + namespace := "" + registerer := prometheus.NewRegistry() + s := NewSet(factory, log, namespace, registerer) + + vtxID := ids.NewID([32]byte{1}) + + vdr1 := ids.NewShortID([20]byte{1}) + vdr2 := ids.NewShortID([20]byte{2}) // k = 2 + + vdrs := ids.ShortSet{} + vdrs.Add( + vdr1, + vdr2, + ) + + if s.Len() != 0 { + t.Fatalf("Shouldn't have any active polls yet") + } else if !s.Add(0, vdrs) { + t.Fatalf("Should have been able to add a new poll") + } else if s.Len() != 1 { + t.Fatalf("Should only have one active poll") + } else if s.Add(0, vdrs) { + t.Fatalf("Shouldn't have been able to add a duplicated poll") + } else if s.Len() != 1 { + t.Fatalf("Should only have one active poll") + } else if _, finished := s.Vote(1, vdr1, vtxID); finished { + t.Fatalf("Shouldn't have been able to finish a non-existant poll") + } else if _, finished := s.Vote(0, vdr1, vtxID); finished { + t.Fatalf("Shouldn't have been able to finish an ongoing poll") + } else if _, finished := s.Vote(0, vdr1, vtxID); finished { + t.Fatalf("Should have dropped a duplicated poll") + } else if result, finished := s.Vote(0, vdr2, vtxID); !finished { + t.Fatalf("Should have finished the") + } else if list := result.List(); len(list) != 1 { + t.Fatalf("Wrong number of vertices returned") + } else if retVtxID := list[0]; !retVtxID.Equals(vtxID) { + t.Fatalf("Wrong vertex returned") + } else if result.Count(vtxID) != 2 { + t.Fatalf("Wrong number of votes returned") + } +} + +func TestCreateAndFinishFailedPoll(t *testing.T) { + factory := NewNoEarlyTermFactory() + log := logging.NoLog{} + namespace := "" + registerer := prometheus.NewRegistry() + s := NewSet(factory, log, namespace, registerer) + + vdr1 := ids.NewShortID([20]byte{1}) + vdr2 := ids.NewShortID([20]byte{2}) // k = 2 + + vdrs := ids.ShortSet{} + vdrs.Add( + vdr1, + vdr2, + ) + + if s.Len() != 0 { + t.Fatalf("Shouldn't have any active polls yet") + } else if !s.Add(0, vdrs) { + t.Fatalf("Should have been able to add a new poll") + } else if s.Len() != 1 { + t.Fatalf("Should only have one active poll") + } else if s.Add(0, vdrs) { + t.Fatalf("Shouldn't have been able to add a duplicated poll") + } else if s.Len() != 1 { + t.Fatalf("Should only have one active poll") + } else if _, finished := s.Drop(1, vdr1); finished { + t.Fatalf("Shouldn't have been able to finish a non-existant poll") + } else if _, finished := s.Drop(0, vdr1); finished { + t.Fatalf("Shouldn't have been able to finish an ongoing poll") + } else if _, finished := s.Drop(0, vdr1); finished { + t.Fatalf("Should have dropped a duplicated poll") + } else if result, finished := s.Drop(0, vdr2); !finished { + t.Fatalf("Should have finished the") + } else if list := result.List(); len(list) != 0 { + t.Fatalf("Wrong number of vertices returned") + } +} + +func TestSetString(t *testing.T) { + factory := NewNoEarlyTermFactory() + log := logging.NoLog{} + namespace := "" + registerer := prometheus.NewRegistry() + s := NewSet(factory, log, namespace, registerer) + + vdr1 := ids.NewShortID([20]byte{1}) // k = 1 + + vdrs := ids.ShortSet{} + vdrs.Add(vdr1) + + expected := "current polls: (Size = 1)\n" + + " 0: waiting on {6HgC8KRBEhXYbF4riJyJFLSHt37UNuRt}" + if !s.Add(0, vdrs) { + t.Fatalf("Should have been able to add a new poll") + } else if str := s.String(); expected != str { + t.Fatalf("Set return wrong string, Expected:\n%s\nReturned:\n%s", + expected, + str) + } +} diff --git a/snow/engine/snowman/polls.go b/snow/engine/snowman/polls.go deleted file mode 100644 index 6e666dc..0000000 --- a/snow/engine/snowman/polls.go +++ /dev/null @@ -1,118 +0,0 @@ -// (c) 2019-2020, Ava Labs, Inc. All rights reserved. -// See the file LICENSE for licensing terms. - -package snowman - -import ( - "fmt" - "strings" - - "github.com/ava-labs/gecko/ids" - "github.com/ava-labs/gecko/utils/logging" - "github.com/prometheus/client_golang/prometheus" -) - -type polls struct { - log logging.Logger - numPolls prometheus.Gauge - alpha int - m map[uint32]poll -} - -// Add to the current set of polls -// Returns true if the poll was registered correctly and the network sample -// should be made. -func (p *polls) Add(requestID uint32, numPolled int) bool { - poll, exists := p.m[requestID] - if !exists { - poll.alpha = p.alpha - poll.numPolled = numPolled - p.m[requestID] = poll - - p.numPolls.Set(float64(len(p.m))) // Tracks performance statistics - } - return !exists -} - -// Vote registers the connections response to a query for [id]. If there was no -// query, or the response has already be registered, nothing is performed. -func (p *polls) Vote(requestID uint32, vdr ids.ShortID, vote ids.ID) (ids.Bag, bool) { - p.log.Verbo("[polls.Vote] Vote: requestID: %d. validatorID: %s. Vote: %s", requestID, vdr, vote) - poll, exists := p.m[requestID] - if !exists { - return ids.Bag{}, false - } - poll.Vote(vote) - if poll.Finished() { - delete(p.m, requestID) - p.numPolls.Set(float64(len(p.m))) // Tracks performance statistics - return poll.votes, true - } - p.m[requestID] = poll - return ids.Bag{}, false -} - -// CancelVote registers the connections failure to respond to a query for [id]. -func (p *polls) CancelVote(requestID uint32, vdr ids.ShortID) (ids.Bag, bool) { - p.log.Verbo("CancelVote received. requestID: %d. validatorID: %s. Vote: %s", requestID, vdr) - poll, exists := p.m[requestID] - if !exists { - return ids.Bag{}, false - } - - poll.CancelVote() - if poll.Finished() { - delete(p.m, requestID) - p.numPolls.Set(float64(len(p.m))) // Tracks performance statistics - return poll.votes, true - } - p.m[requestID] = poll - return ids.Bag{}, false -} - -func (p *polls) String() string { - sb := strings.Builder{} - - sb.WriteString(fmt.Sprintf("Current polls: (Size = %d)", len(p.m))) - for requestID, poll := range p.m { - sb.WriteString(fmt.Sprintf("\n %d: %s", requestID, poll)) - } - - return sb.String() -} - -// poll represents the current state of a network poll for a block -type poll struct { - alpha int - votes ids.Bag - numPolled int -} - -// Vote registers a vote for this poll -func (p *poll) CancelVote() { - if p.numPolled > 0 { - p.numPolled-- - } -} - -// Vote registers a vote for this poll -func (p *poll) Vote(vote ids.ID) { - if p.numPolled > 0 { - p.numPolled-- - p.votes.Add(vote) - } -} - -// Finished returns true if the poll has completed, with no more required -// responses -func (p poll) Finished() bool { - received := p.votes.Len() - _, freq := p.votes.Mode() - return p.numPolled == 0 || // All k nodes responded - freq >= p.alpha || // An alpha majority has returned - received+p.numPolled < p.alpha // An alpha majority can never return -} - -func (p poll) String() string { - return fmt.Sprintf("Waiting on %d chits", p.numPolled) -} diff --git a/snow/engine/snowman/transitive.go b/snow/engine/snowman/transitive.go index f155f5e..6b57a8f 100644 --- a/snow/engine/snowman/transitive.go +++ b/snow/engine/snowman/transitive.go @@ -12,6 +12,7 @@ import ( "github.com/ava-labs/gecko/snow/choices" "github.com/ava-labs/gecko/snow/consensus/snowman" "github.com/ava-labs/gecko/snow/engine/common" + "github.com/ava-labs/gecko/snow/engine/snowman/poll" "github.com/ava-labs/gecko/snow/events" "github.com/ava-labs/gecko/utils/formatting" "github.com/ava-labs/gecko/utils/wrappers" @@ -30,7 +31,7 @@ type Transitive struct { bootstrapper // track outstanding preference requests - polls polls + polls poll.Set // blocks that have outstanding get requests blkReqs common.Requests @@ -64,10 +65,12 @@ func (t *Transitive) Initialize(config Config) error { t.onFinished = t.finishBootstrapping - t.polls.log = config.Context.Log - t.polls.numPolls = t.numPolls - t.polls.alpha = t.Params.Alpha - t.polls.m = make(map[uint32]poll) + factory := poll.NewEarlyTermNoTraversalFactory(int(config.Params.Alpha)) + t.polls = poll.NewSet(factory, + config.Context.Log, + config.Params.Namespace, + config.Params.Metrics, + ) return t.bootstrapper.Initialize(config.BootstrapConfig) } @@ -185,7 +188,11 @@ func (t *Transitive) GetAncestors(vdr ids.ShortID, requestID uint32, blkID ids.I func (t *Transitive) Put(vdr ids.ShortID, requestID uint32, blkID ids.ID, blkBytes []byte) error { // bootstrapping isn't done --> we didn't send any gets --> this put is invalid if !t.bootstrapped { - t.Config.Context.Log.Debug("dropping Put(%s, %d, %s) due to bootstrapping", vdr, requestID, blkID) + if requestID == network.GossipMsgRequestID { + t.Config.Context.Log.Verbo("dropping gossip Put(%s, %d, %s) due to bootstrapping", vdr, requestID, blkID) + } else { + t.Config.Context.Log.Debug("dropping Put(%s, %d, %s) due to bootstrapping", vdr, requestID, blkID) + } return nil } @@ -405,7 +412,7 @@ func (t *Transitive) repoll() { // propagate the most likely branch as quickly as possible prefID := t.Consensus.Preference() - for i := len(t.polls.m); i < t.Params.ConcurrentRepolls; i++ { + for i := t.polls.Len(); i < t.Params.ConcurrentRepolls; i++ { t.pullSample(prefID) } } @@ -542,18 +549,15 @@ func (t *Transitive) pullSample(blkID ids.ID) { vdrSet.Add(vdr.ID()) } - if numVdrs := len(vdrs); numVdrs != p.K { - t.Config.Context.Log.Error("query for %s was dropped due to an insufficient number of validators", blkID) - return - } + toSample := ids.ShortSet{} + toSample.Union(vdrSet) t.RequestID++ - if !t.polls.Add(t.RequestID, vdrSet.Len()) { - t.Config.Context.Log.Error("query for %s was dropped due to use of a duplicated requestID", blkID) - return + if numVdrs := len(vdrs); numVdrs == p.K && t.polls.Add(t.RequestID, vdrSet) { + t.Config.Sender.PullQuery(toSample, t.RequestID, blkID) + } else if numVdrs < p.K { + t.Config.Context.Log.Error("query for %s was dropped due to an insufficient number of validators", blkID) } - - t.Config.Sender.PullQuery(vdrSet, t.RequestID, blkID) } // send a push request for this block @@ -566,20 +570,15 @@ func (t *Transitive) pushSample(blk snowman.Block) { vdrSet.Add(vdr.ID()) } - blkID := blk.ID() - if numVdrs := len(vdrs); numVdrs != p.K { - t.Config.Context.Log.Error("query for %s was dropped due to an insufficient number of validators", blkID) - return - } + toSample := ids.ShortSet{} + toSample.Union(vdrSet) t.RequestID++ - if !t.polls.Add(t.RequestID, vdrSet.Len()) { - t.Config.Context.Log.Error("query for %s was dropped due to use of a duplicated requestID", blkID) - return + if numVdrs := len(vdrs); numVdrs == p.K && t.polls.Add(t.RequestID, vdrSet) { + t.Config.Sender.PushQuery(toSample, t.RequestID, blk.ID(), blk.Bytes()) + } else if numVdrs < p.K { + t.Config.Context.Log.Error("query for %s was dropped due to an insufficient number of validators", blk.ID()) } - - t.Config.Sender.PushQuery(vdrSet, t.RequestID, blkID, blk.Bytes()) - return } func (t *Transitive) deliver(blk snowman.Block) error { @@ -648,3 +647,8 @@ func (t *Transitive) deliver(blk snowman.Block) error { t.numBlockedBlk.Set(float64(t.pending.Len())) return t.errs.Err } + +// IsBootstrapped returns true iff this chain is done bootstrapping +func (t *Transitive) IsBootstrapped() bool { + return t.bootstrapped +} diff --git a/snow/engine/snowman/transitive_test.go b/snow/engine/snowman/transitive_test.go index a3db1d6..e9571af 100644 --- a/snow/engine/snowman/transitive_test.go +++ b/snow/engine/snowman/transitive_test.go @@ -810,13 +810,13 @@ func TestVoteCanceling(t *testing.T) { te.insert(blk) - if len(te.polls.m) != 1 { + if te.polls.Len() != 1 { t.Fatalf("Shouldn't have finished blocking issue") } te.QueryFailed(vdr0.ID(), *queryRequestID) - if len(te.polls.m) != 1 { + if te.polls.Len() != 1 { t.Fatalf("Shouldn't have finished blocking issue") } @@ -1522,3 +1522,124 @@ func TestEngineAggressivePolling(t *testing.T) { t.Fatalf("Should have sent an additional pull query") } } + +func TestEngineDoubleChit(t *testing.T) { + config := DefaultConfig() + + config.Params = snowball.Parameters{ + Metrics: prometheus.NewRegistry(), + K: 2, + Alpha: 2, + BetaVirtuous: 1, + BetaRogue: 2, + } + + vdr0 := validators.GenerateRandomValidator(1) + vdr1 := validators.GenerateRandomValidator(1) + + vals := validators.NewSet() + config.Validators = vals + + vals.Add(vdr0) + vals.Add(vdr1) + + sender := &common.SenderTest{} + sender.T = t + config.Sender = sender + + sender.Default(true) + + vm := &VMTest{} + vm.T = t + config.VM = vm + + vm.Default(true) + vm.CantSetPreference = false + + gBlk := &Blk{ + id: GenerateID(), + status: choices.Accepted, + } + + vm.LastAcceptedF = func() ids.ID { return gBlk.ID() } + sender.CantGetAcceptedFrontier = false + + vm.GetBlockF = func(id ids.ID) (snowman.Block, error) { + switch { + case id.Equals(gBlk.ID()): + return gBlk, nil + } + t.Fatalf("Unknown block") + panic("Should have errored") + } + + te := &Transitive{} + te.Initialize(config) + te.finishBootstrapping() + + vm.LastAcceptedF = nil + sender.CantGetAcceptedFrontier = true + + blk := &Blk{ + parent: gBlk, + id: GenerateID(), + status: choices.Processing, + bytes: []byte{1}, + } + + queried := new(bool) + queryRequestID := new(uint32) + sender.PushQueryF = func(inVdrs ids.ShortSet, requestID uint32, blkID ids.ID, blkBytes []byte) { + if *queried { + t.Fatalf("Asked multiple times") + } + *queried = true + *queryRequestID = requestID + vdrSet := ids.ShortSet{} + vdrSet.Add(vdr0.ID(), vdr1.ID()) + if !inVdrs.Equals(vdrSet) { + t.Fatalf("Asking wrong validator for preference") + } + if !blk.ID().Equals(blkID) { + t.Fatalf("Asking for wrong block") + } + } + + te.insert(blk) + + vm.GetBlockF = func(id ids.ID) (snowman.Block, error) { + switch { + case id.Equals(gBlk.ID()): + return gBlk, nil + case id.Equals(blk.ID()): + return blk, nil + } + t.Fatalf("Unknown block") + panic("Should have errored") + } + + blkSet := ids.Set{} + blkSet.Add(blk.ID()) + + if status := blk.Status(); status != choices.Processing { + t.Fatalf("Wrong status: %s ; expected: %s", status, choices.Processing) + } + + te.Chits(vdr0.ID(), *queryRequestID, blkSet) + + if status := blk.Status(); status != choices.Processing { + t.Fatalf("Wrong status: %s ; expected: %s", status, choices.Processing) + } + + te.Chits(vdr0.ID(), *queryRequestID, blkSet) + + if status := blk.Status(); status != choices.Processing { + t.Fatalf("Wrong status: %s ; expected: %s", status, choices.Processing) + } + + te.Chits(vdr1.ID(), *queryRequestID, blkSet) + + if status := blk.Status(); status != choices.Accepted { + t.Fatalf("Wrong status: %s ; expected: %s", status, choices.Accepted) + } +} diff --git a/snow/engine/snowman/voter.go b/snow/engine/snowman/voter.go index bd15831..5a3ca87 100644 --- a/snow/engine/snowman/voter.go +++ b/snow/engine/snowman/voter.go @@ -32,7 +32,7 @@ func (v *voter) Update() { results := ids.Bag{} finished := false if v.response.IsZero() { - results, finished = v.t.polls.CancelVote(v.requestID, v.vdr) + results, finished = v.t.polls.Drop(v.requestID, v.vdr) } else { results, finished = v.t.polls.Vote(v.requestID, v.vdr, v.response) } diff --git a/snow/events/blocker.go b/snow/events/blocker.go index 6bfdd7b..3491b24 100644 --- a/snow/events/blocker.go +++ b/snow/events/blocker.go @@ -10,12 +10,16 @@ import ( "github.com/ava-labs/gecko/ids" ) +const ( + minBlockerSize = 16 +) + // Blocker tracks objects that are blocked type Blocker map[[32]byte][]Blockable func (b *Blocker) init() { if *b == nil { - *b = make(map[[32]byte][]Blockable) + *b = make(map[[32]byte][]Blockable, minBlockerSize) } } diff --git a/snow/networking/router/subnet_router.go b/snow/networking/router/chain_router.go similarity index 79% rename from snow/networking/router/subnet_router.go rename to snow/networking/router/chain_router.go index 5bf977c..bbb345d 100644 --- a/snow/networking/router/subnet_router.go +++ b/snow/networking/router/chain_router.go @@ -7,6 +7,8 @@ import ( "sync" "time" + "github.com/ava-labs/gecko/utils/formatting" + "github.com/ava-labs/gecko/ids" "github.com/ava-labs/gecko/snow/networking/timeout" "github.com/ava-labs/gecko/utils/logging" @@ -67,7 +69,7 @@ func (sr *ChainRouter) RemoveChain(chainID ids.ID) { sr.lock.RLock() chain, exists := sr.chains[chainID.Key()] if !exists { - sr.log.Debug("message referenced a chain, %s, this node doesn't validate", chainID) + sr.log.Debug("can't remove unknown chain %s", chainID) sr.lock.RUnlock() return } @@ -95,7 +97,7 @@ func (sr *ChainRouter) GetAcceptedFrontier(validatorID ids.ShortID, chainID ids. if chain, exists := sr.chains[chainID.Key()]; exists { chain.GetAcceptedFrontier(validatorID, requestID) } else { - sr.log.Debug("message referenced a chain, %s, this node doesn't validate", chainID) + sr.log.Debug("GetAcceptedFrontier(%s, %s, %d) dropped due to unknown chain", validatorID, chainID, requestID) } } @@ -111,7 +113,7 @@ func (sr *ChainRouter) AcceptedFrontier(validatorID ids.ShortID, chainID ids.ID, sr.timeouts.Cancel(validatorID, chainID, requestID) } } else { - sr.log.Debug("message referenced a chain, %s, this node doesn't validate", chainID) + sr.log.Debug("AcceptedFrontier(%s, %s, %d, %s) dropped due to unknown chain", validatorID, chainID, requestID, containerIDs) } } @@ -122,19 +124,12 @@ func (sr *ChainRouter) GetAcceptedFrontierFailed(validatorID ids.ShortID, chainI sr.lock.RLock() defer sr.lock.RUnlock() - if chain, exists := sr.chains[chainID.Key()]; exists { - if !chain.GetAcceptedFrontierFailed(validatorID, requestID) { - sr.log.Debug("deferring GetAcceptedFrontier timeout due to a full queue on %s", chainID) - // Defer this call to later - sr.timeouts.Register(validatorID, chainID, requestID, func() { - sr.GetAcceptedFrontierFailed(validatorID, chainID, requestID) - }) - return - } - } else { - sr.log.Debug("message referenced a chain, %s, this node doesn't validate", chainID) - } sr.timeouts.Cancel(validatorID, chainID, requestID) + if chain, exists := sr.chains[chainID.Key()]; exists { + chain.GetAcceptedFrontierFailed(validatorID, requestID) + } else { + sr.log.Error("GetAcceptedFrontierFailed(%s, %s, %d) dropped due to unknown chain", validatorID, chainID, requestID) + } } // GetAccepted routes an incoming GetAccepted request from the @@ -147,7 +142,7 @@ func (sr *ChainRouter) GetAccepted(validatorID ids.ShortID, chainID ids.ID, requ if chain, exists := sr.chains[chainID.Key()]; exists { chain.GetAccepted(validatorID, requestID, containerIDs) } else { - sr.log.Debug("message referenced a chain, %s, this node doesn't validate", chainID) + sr.log.Debug("GetAccepted(%s, %s, %d, %s) dropped due to unknown chain", validatorID, chainID, requestID, containerIDs) } } @@ -163,7 +158,7 @@ func (sr *ChainRouter) Accepted(validatorID ids.ShortID, chainID ids.ID, request sr.timeouts.Cancel(validatorID, chainID, requestID) } } else { - sr.log.Debug("message referenced a chain, %s, this node doesn't validate", chainID) + sr.log.Debug("Accepted(%s, %s, %d, %s) dropped due to unknown chain", validatorID, chainID, requestID, containerIDs) } } @@ -174,18 +169,12 @@ func (sr *ChainRouter) GetAcceptedFailed(validatorID ids.ShortID, chainID ids.ID sr.lock.RLock() defer sr.lock.RUnlock() - if chain, exists := sr.chains[chainID.Key()]; exists { - if !chain.GetAcceptedFailed(validatorID, requestID) { - sr.timeouts.Register(validatorID, chainID, requestID, func() { - sr.log.Debug("deferring GetAccepted timeout due to a full queue on %s", chainID) - sr.GetAcceptedFailed(validatorID, chainID, requestID) - }) - return - } - } else { - sr.log.Debug("message referenced a chain, %s, this node doesn't validate", chainID) - } sr.timeouts.Cancel(validatorID, chainID, requestID) + if chain, exists := sr.chains[chainID.Key()]; exists { + chain.GetAcceptedFailed(validatorID, requestID) + } else { + sr.log.Error("GetAcceptedFailed(%s, %s, %d) dropped due to unknown chain", validatorID, chainID, requestID) + } } // GetAncestors routes an incoming GetAncestors message from the validator with ID [validatorID] @@ -198,7 +187,7 @@ func (sr *ChainRouter) GetAncestors(validatorID ids.ShortID, chainID ids.ID, req if chain, exists := sr.chains[chainID.Key()]; exists { chain.GetAncestors(validatorID, requestID, containerID) } else { - sr.log.Debug("message referenced a chain, %s, this node doesn't validate", chainID) + sr.log.Debug("GetAncestors(%s, %s, %d) dropped due to unknown chain", validatorID, chainID, requestID) } } @@ -215,7 +204,7 @@ func (sr *ChainRouter) MultiPut(validatorID ids.ShortID, chainID ids.ID, request sr.timeouts.Cancel(validatorID, chainID, requestID) } } else { - sr.log.Debug("message referenced a chain, %s, this node doesn't validate", chainID) + sr.log.Debug("MultiPut(%s, %s, %d, %d) dropped due to unknown chain", validatorID, chainID, requestID, len(containers)) } } @@ -225,18 +214,12 @@ func (sr *ChainRouter) GetAncestorsFailed(validatorID ids.ShortID, chainID ids.I sr.lock.RLock() defer sr.lock.RUnlock() - if chain, exists := sr.chains[chainID.Key()]; exists { - if !chain.GetAncestorsFailed(validatorID, requestID) { - sr.timeouts.Register(validatorID, chainID, requestID, func() { - sr.log.Debug("deferring GetAncestors timeout due to a full queue on %s", chainID) - sr.GetAncestorsFailed(validatorID, chainID, requestID) - }) - return - } - } else { - sr.log.Debug("message referenced a chain, %s, this node doesn't validate", chainID) - } sr.timeouts.Cancel(validatorID, chainID, requestID) + if chain, exists := sr.chains[chainID.Key()]; exists { + chain.GetAncestorsFailed(validatorID, requestID) + } else { + sr.log.Error("GetAncestorsFailed(%s, %s, %d, %d) dropped due to unknown chain", validatorID, chainID, requestID) + } } // Get routes an incoming Get request from the validator with ID [validatorID] @@ -248,7 +231,7 @@ func (sr *ChainRouter) Get(validatorID ids.ShortID, chainID ids.ID, requestID ui if chain, exists := sr.chains[chainID.Key()]; exists { chain.Get(validatorID, requestID, containerID) } else { - sr.log.Debug("message referenced a chain, %s, this node doesn't validate", chainID) + sr.log.Debug("Get(%s, %s, %d, %s) dropped due to unknown chain", validatorID, chainID, requestID, containerID) } } @@ -265,7 +248,8 @@ func (sr *ChainRouter) Put(validatorID ids.ShortID, chainID ids.ID, requestID ui sr.timeouts.Cancel(validatorID, chainID, requestID) } } else { - sr.log.Debug("message referenced a chain, %s, this node doesn't validate", chainID) + sr.log.Debug("Put(%s, %s, %d, %s) dropped due to unknown chain", validatorID, chainID, requestID, containerID) + sr.log.Verbo("container:\n%s", formatting.DumpBytes{Bytes: container}) } } @@ -275,18 +259,12 @@ func (sr *ChainRouter) GetFailed(validatorID ids.ShortID, chainID ids.ID, reques sr.lock.RLock() defer sr.lock.RUnlock() - if chain, exists := sr.chains[chainID.Key()]; exists { - if !chain.GetFailed(validatorID, requestID) { - sr.timeouts.Register(validatorID, chainID, requestID, func() { - sr.log.Debug("deferring Get timeout due to a full queue on %s", chainID) - sr.GetFailed(validatorID, chainID, requestID) - }) - return - } - } else { - sr.log.Debug("message referenced a chain, %s, this node doesn't validate", chainID) - } sr.timeouts.Cancel(validatorID, chainID, requestID) + if chain, exists := sr.chains[chainID.Key()]; exists { + chain.GetFailed(validatorID, requestID) + } else { + sr.log.Error("GetFailed(%s, %s, %d) dropped due to unknown chain", validatorID, chainID, requestID) + } } // PushQuery routes an incoming PushQuery request from the validator with ID [validatorID] @@ -298,7 +276,8 @@ func (sr *ChainRouter) PushQuery(validatorID ids.ShortID, chainID ids.ID, reques if chain, exists := sr.chains[chainID.Key()]; exists { chain.PushQuery(validatorID, requestID, containerID, container) } else { - sr.log.Debug("message referenced a chain, %s, this node doesn't validate", chainID) + sr.log.Debug("PushQuery(%s, %s, %d, %s) dropped due to unknown chain", validatorID, chainID, requestID, containerID) + sr.log.Verbo("container:\n%s", formatting.DumpBytes{Bytes: container}) } } @@ -311,7 +290,7 @@ func (sr *ChainRouter) PullQuery(validatorID ids.ShortID, chainID ids.ID, reques if chain, exists := sr.chains[chainID.Key()]; exists { chain.PullQuery(validatorID, requestID, containerID) } else { - sr.log.Debug("message referenced a chain, %s, this node doesn't validate", chainID) + sr.log.Debug("PullQuery(%s, %s, %d, %s) dropped due to unknown chain", validatorID, chainID, requestID, containerID) } } @@ -327,7 +306,7 @@ func (sr *ChainRouter) Chits(validatorID ids.ShortID, chainID ids.ID, requestID sr.timeouts.Cancel(validatorID, chainID, requestID) } } else { - sr.log.Debug("message referenced a chain, %s, this node doesn't validate", chainID) + sr.log.Debug("Chits(%s, %s, %d, %s) dropped due to unknown chain", validatorID, chainID, requestID, votes) } } @@ -337,18 +316,12 @@ func (sr *ChainRouter) QueryFailed(validatorID ids.ShortID, chainID ids.ID, requ sr.lock.RLock() defer sr.lock.RUnlock() - if chain, exists := sr.chains[chainID.Key()]; exists { - if !chain.QueryFailed(validatorID, requestID) { - sr.timeouts.Register(validatorID, chainID, requestID, func() { - sr.log.Debug("deferring Query timeout due to a full queue on %s", chainID) - sr.QueryFailed(validatorID, chainID, requestID) - }) - return - } - } else { - sr.log.Debug("message referenced a chain, %s, this node doesn't validate", chainID) - } sr.timeouts.Cancel(validatorID, chainID, requestID) + if chain, exists := sr.chains[chainID.Key()]; exists { + chain.QueryFailed(validatorID, requestID) + } else { + sr.log.Error("QueryFailed(%s, %s, %d) dropped due to unknown chain", validatorID, chainID, requestID) + } } // Shutdown shuts down this router diff --git a/snow/networking/router/handler.go b/snow/networking/router/handler.go index 8b40223..03a55f1 100644 --- a/snow/networking/router/handler.go +++ b/snow/networking/router/handler.go @@ -4,12 +4,14 @@ package router import ( + "sync" "time" + "github.com/prometheus/client_golang/prometheus" + "github.com/ava-labs/gecko/ids" "github.com/ava-labs/gecko/snow" "github.com/ava-labs/gecko/snow/engine/common" - "github.com/prometheus/client_golang/prometheus" ) // Handler passes incoming messages from the network to the consensus engine @@ -17,12 +19,18 @@ import ( type Handler struct { metrics - msgs chan message - closed chan struct{} - engine common.Engine - msgChan <-chan common.Message + msgs chan message + reliableMsgsSema chan struct{} + reliableMsgsLock sync.Mutex + reliableMsgs []message + closed chan struct{} + msgChan <-chan common.Message + + ctx *snow.Context + engine common.Engine toClose func() + closing bool } // Initialize this consensus handler @@ -35,48 +43,58 @@ func (h *Handler) Initialize( ) { h.metrics.Initialize(namespace, metrics) h.msgs = make(chan message, bufferSize) + h.reliableMsgsSema = make(chan struct{}, 1) h.closed = make(chan struct{}) - h.engine = engine h.msgChan = msgChan + + h.ctx = engine.Context() + h.engine = engine } // Context of this Handler func (h *Handler) Context() *snow.Context { return h.engine.Context() } +// Engine returns the engine this handler dispatches to +func (h *Handler) Engine() common.Engine { return h.engine } + +// SetEngine sets the engine this handler dispatches to +func (h *Handler) SetEngine(engine common.Engine) { h.engine = engine } + // Dispatch waits for incoming messages from the network // and, when they arrive, sends them to the consensus engine func (h *Handler) Dispatch() { - log := h.Context().Log defer func() { - log.Info("finished shutting down chain") + h.ctx.Log.Info("finished shutting down chain") close(h.closed) }() - closing := false for { select { case msg, ok := <-h.msgs: if !ok { + // the msgs channel has been closed, so this dispatcher should exit return } + h.metrics.pending.Dec() - if closing { - log.Debug("dropping message due to closing:\n%s", msg) - continue - } - if h.dispatchMsg(msg) { - closing = true + h.dispatchMsg(msg) + case <-h.reliableMsgsSema: + // get all the reliable messages + h.reliableMsgsLock.Lock() + msgs := h.reliableMsgs + h.reliableMsgs = nil + h.reliableMsgsLock.Unlock() + + // fire all the reliable messages + for _, msg := range msgs { + h.metrics.pending.Dec() + h.dispatchMsg(msg) } case msg := <-h.msgChan: - if closing { - log.Debug("dropping internal message due to closing:\n%s", msg) - continue - } - if h.dispatchMsg(message{messageType: notifyMsg, notification: msg}) { - closing = true - } + // handle a message from the VM + h.dispatchMsg(message{messageType: notifyMsg, notification: msg}) } - if closing && h.toClose != nil { + if h.closing && h.toClose != nil { go h.toClose() } } @@ -85,14 +103,19 @@ func (h *Handler) Dispatch() { // Dispatch a message to the consensus engine. // Returns true iff this consensus handler (and its associated engine) should shutdown // (due to receipt of a shutdown message) -func (h *Handler) dispatchMsg(msg message) bool { +func (h *Handler) dispatchMsg(msg message) { + if h.closing { + h.ctx.Log.Debug("dropping message due to closing:\n%s", msg) + h.metrics.dropped.Inc() + return + } + startTime := time.Now() - ctx := h.engine.Context() - ctx.Lock.Lock() - defer ctx.Lock.Unlock() + h.ctx.Lock.Lock() + defer h.ctx.Lock.Unlock() - ctx.Log.Verbo("Forwarding message to consensus: %s", msg) + h.ctx.Log.Verbo("Forwarding message to consensus: %s", msg) var ( err error done bool @@ -159,9 +182,10 @@ func (h *Handler) dispatchMsg(msg message) bool { } if err != nil { - ctx.Log.Fatal("forcing chain to shutdown due to %s", err) + h.ctx.Log.Fatal("forcing chain to shutdown due to %s", err) } - return done || err != nil + + h.closing = done || err != nil } // GetAcceptedFrontier passes a GetAcceptedFrontier message received from the @@ -187,8 +211,8 @@ func (h *Handler) AcceptedFrontier(validatorID ids.ShortID, requestID uint32, co // GetAcceptedFrontierFailed passes a GetAcceptedFrontierFailed message received // from the network to the consensus engine. -func (h *Handler) GetAcceptedFrontierFailed(validatorID ids.ShortID, requestID uint32) bool { - return h.sendMsg(message{ +func (h *Handler) GetAcceptedFrontierFailed(validatorID ids.ShortID, requestID uint32) { + h.sendReliableMsg(message{ messageType: getAcceptedFrontierFailedMsg, validatorID: validatorID, requestID: requestID, @@ -219,14 +243,43 @@ func (h *Handler) Accepted(validatorID ids.ShortID, requestID uint32, containerI // GetAcceptedFailed passes a GetAcceptedFailed message received from the // network to the consensus engine. -func (h *Handler) GetAcceptedFailed(validatorID ids.ShortID, requestID uint32) bool { - return h.sendMsg(message{ +func (h *Handler) GetAcceptedFailed(validatorID ids.ShortID, requestID uint32) { + h.sendReliableMsg(message{ messageType: getAcceptedFailedMsg, validatorID: validatorID, requestID: requestID, }) } +// GetAncestors passes a GetAncestors message received from the network to the consensus engine. +func (h *Handler) GetAncestors(validatorID ids.ShortID, requestID uint32, containerID ids.ID) bool { + return h.sendMsg(message{ + messageType: getAncestorsMsg, + validatorID: validatorID, + requestID: requestID, + containerID: containerID, + }) +} + +// MultiPut passes a MultiPut message received from the network to the consensus engine. +func (h *Handler) MultiPut(validatorID ids.ShortID, requestID uint32, containers [][]byte) bool { + return h.sendMsg(message{ + messageType: multiPutMsg, + validatorID: validatorID, + requestID: requestID, + containers: containers, + }) +} + +// GetAncestorsFailed passes a GetAncestorsFailed message to the consensus engine. +func (h *Handler) GetAncestorsFailed(validatorID ids.ShortID, requestID uint32) { + h.sendReliableMsg(message{ + messageType: getAncestorsFailedMsg, + validatorID: validatorID, + requestID: requestID, + }) +} + // Get passes a Get message received from the network to the consensus engine. func (h *Handler) Get(validatorID ids.ShortID, requestID uint32, containerID ids.ID) bool { return h.sendMsg(message{ @@ -237,16 +290,6 @@ func (h *Handler) Get(validatorID ids.ShortID, requestID uint32, containerID ids }) } -// GetAncestors passes a GetAncestors message received from the network to the consensus engine. -func (h *Handler) GetAncestors(validatorID ids.ShortID, requestID uint32, containerID ids.ID) bool { - return h.sendMsg(message{ - messageType: getAncestorsMsg, - validatorID: validatorID, - requestID: requestID, - containerID: containerID, - }) -} - // Put passes a Put message received from the network to the consensus engine. func (h *Handler) Put(validatorID ids.ShortID, requestID uint32, containerID ids.ID, container []byte) bool { return h.sendMsg(message{ @@ -258,34 +301,15 @@ func (h *Handler) Put(validatorID ids.ShortID, requestID uint32, containerID ids }) } -// MultiPut passes a MultiPut message received from the network to the consensus engine. -func (h *Handler) MultiPut(validatorID ids.ShortID, requestID uint32, containers [][]byte) bool { - return h.sendMsg(message{ - messageType: multiPutMsg, - validatorID: validatorID, - requestID: requestID, - containers: containers, - }) -} - // GetFailed passes a GetFailed message to the consensus engine. -func (h *Handler) GetFailed(validatorID ids.ShortID, requestID uint32) bool { - return h.sendMsg(message{ +func (h *Handler) GetFailed(validatorID ids.ShortID, requestID uint32) { + h.sendReliableMsg(message{ messageType: getFailedMsg, validatorID: validatorID, requestID: requestID, }) } -// GetAncestorsFailed passes a GetAncestorsFailed message to the consensus engine. -func (h *Handler) GetAncestorsFailed(validatorID ids.ShortID, requestID uint32) bool { - return h.sendMsg(message{ - messageType: getAncestorsFailedMsg, - validatorID: validatorID, - requestID: requestID, - }) -} - // PushQuery passes a PushQuery message received from the network to the consensus engine. func (h *Handler) PushQuery(validatorID ids.ShortID, requestID uint32, blockID ids.ID, block []byte) bool { return h.sendMsg(message{ @@ -318,8 +342,8 @@ func (h *Handler) Chits(validatorID ids.ShortID, requestID uint32, votes ids.Set } // QueryFailed passes a QueryFailed message received from the network to the consensus engine. -func (h *Handler) QueryFailed(validatorID ids.ShortID, requestID uint32) bool { - return h.sendMsg(message{ +func (h *Handler) QueryFailed(validatorID ids.ShortID, requestID uint32) { + h.sendReliableMsg(message{ messageType: queryFailedMsg, validatorID: validatorID, requestID: requestID, @@ -341,8 +365,9 @@ func (h *Handler) Notify(msg common.Message) bool { // Shutdown shuts down the dispatcher func (h *Handler) Shutdown() { - h.metrics.pending.Inc() - h.msgs <- message{messageType: shutdownMsg} + h.sendReliableMsg(message{ + messageType: shutdownMsg, + }) } func (h *Handler) sendMsg(msg message) bool { @@ -355,3 +380,15 @@ func (h *Handler) sendMsg(msg message) bool { return false } } + +func (h *Handler) sendReliableMsg(msg message) { + h.reliableMsgsLock.Lock() + defer h.reliableMsgsLock.Unlock() + + h.metrics.pending.Inc() + h.reliableMsgs = append(h.reliableMsgs, msg) + select { + case h.reliableMsgsSema <- struct{}{}: + default: + } +} diff --git a/snow/networking/sender/sender.go b/snow/networking/sender/sender.go index e81c5c0..92c02b8 100644 --- a/snow/networking/sender/sender.go +++ b/snow/networking/sender/sender.go @@ -31,17 +31,16 @@ func (s *Sender) Context() *snow.Context { return s.ctx } // GetAcceptedFrontier ... func (s *Sender) GetAcceptedFrontier(validatorIDs ids.ShortSet, requestID uint32) { - if validatorIDs.Contains(s.ctx.NodeID) { - validatorIDs.Remove(s.ctx.NodeID) - go s.router.GetAcceptedFrontier(s.ctx.NodeID, s.ctx.ChainID, requestID) - } - validatorList := validatorIDs.List() - for _, validatorID := range validatorList { + for _, validatorID := range validatorIDs.List() { vID := validatorID s.timeouts.Register(validatorID, s.ctx.ChainID, requestID, func() { s.router.GetAcceptedFrontierFailed(vID, s.ctx.ChainID, requestID) }) } + if validatorIDs.Contains(s.ctx.NodeID) { + validatorIDs.Remove(s.ctx.NodeID) + go s.router.GetAcceptedFrontier(s.ctx.NodeID, s.ctx.ChainID, requestID) + } s.sender.GetAcceptedFrontier(validatorIDs, s.ctx.ChainID, requestID) } @@ -49,24 +48,23 @@ func (s *Sender) GetAcceptedFrontier(validatorIDs ids.ShortSet, requestID uint32 func (s *Sender) AcceptedFrontier(validatorID ids.ShortID, requestID uint32, containerIDs ids.Set) { if validatorID.Equals(s.ctx.NodeID) { go s.router.AcceptedFrontier(validatorID, s.ctx.ChainID, requestID, containerIDs) - return + } else { + s.sender.AcceptedFrontier(validatorID, s.ctx.ChainID, requestID, containerIDs) } - s.sender.AcceptedFrontier(validatorID, s.ctx.ChainID, requestID, containerIDs) } // GetAccepted ... func (s *Sender) GetAccepted(validatorIDs ids.ShortSet, requestID uint32, containerIDs ids.Set) { - if validatorIDs.Contains(s.ctx.NodeID) { - validatorIDs.Remove(s.ctx.NodeID) - go s.router.GetAccepted(s.ctx.NodeID, s.ctx.ChainID, requestID, containerIDs) - } - validatorList := validatorIDs.List() - for _, validatorID := range validatorList { + for _, validatorID := range validatorIDs.List() { vID := validatorID s.timeouts.Register(validatorID, s.ctx.ChainID, requestID, func() { s.router.GetAcceptedFailed(vID, s.ctx.ChainID, requestID) }) } + if validatorIDs.Contains(s.ctx.NodeID) { + validatorIDs.Remove(s.ctx.NodeID) + go s.router.GetAccepted(s.ctx.NodeID, s.ctx.ChainID, requestID, containerIDs) + } s.sender.GetAccepted(validatorIDs, s.ctx.ChainID, requestID, containerIDs) } @@ -74,9 +72,9 @@ func (s *Sender) GetAccepted(validatorIDs ids.ShortSet, requestID uint32, contai func (s *Sender) Accepted(validatorID ids.ShortID, requestID uint32, containerIDs ids.Set) { if validatorID.Equals(s.ctx.NodeID) { go s.router.Accepted(validatorID, s.ctx.ChainID, requestID, containerIDs) - return + } else { + s.sender.Accepted(validatorID, s.ctx.ChainID, requestID, containerIDs) } - s.sender.Accepted(validatorID, s.ctx.ChainID, requestID, containerIDs) } // Get sends a Get message to the consensus engine running on the specified @@ -85,6 +83,13 @@ func (s *Sender) Accepted(validatorID ids.ShortID, requestID uint32, containerID // specified container. func (s *Sender) Get(validatorID ids.ShortID, requestID uint32, containerID ids.ID) { s.ctx.Log.Verbo("Sending Get to validator %s. RequestID: %d. ContainerID: %s", validatorID, requestID, containerID) + + // Sending a Get to myself will always fail + if validatorID.Equals(s.ctx.NodeID) { + go s.router.GetFailed(validatorID, s.ctx.ChainID, requestID) + return + } + // Add a timeout -- if we don't get a response before the timeout expires, // send this consensus engine a GetFailed message s.timeouts.Register(validatorID, s.ctx.ChainID, requestID, func() { @@ -101,6 +106,7 @@ func (s *Sender) GetAncestors(validatorID ids.ShortID, requestID uint32, contain go s.router.GetAncestorsFailed(validatorID, s.ctx.ChainID, requestID) return } + s.timeouts.Register(validatorID, s.ctx.ChainID, requestID, func() { s.router.GetAncestorsFailed(validatorID, s.ctx.ChainID, requestID) }) @@ -130,6 +136,13 @@ func (s *Sender) MultiPut(validatorID ids.ShortID, requestID uint32, containers // their preferred frontier given the existence of the specified container. func (s *Sender) PushQuery(validatorIDs ids.ShortSet, requestID uint32, containerID ids.ID, container []byte) { s.ctx.Log.Verbo("Sending PushQuery to validators %v. RequestID: %d. ContainerID: %s", validatorIDs, requestID, containerID) + for _, validatorID := range validatorIDs.List() { + vID := validatorID + s.timeouts.Register(validatorID, s.ctx.ChainID, requestID, func() { + s.router.QueryFailed(vID, s.ctx.ChainID, requestID) + }) + } + // If one of the validators in [validatorIDs] is myself, send this message directly // to my own router rather than sending it over the network if validatorIDs.Contains(s.ctx.NodeID) { // One of the validators in [validatorIDs] was myself @@ -139,13 +152,7 @@ func (s *Sender) PushQuery(validatorIDs ids.ShortSet, requestID uint32, containe // If this were not a goroutine, then we would deadlock here when [handler].msgs is full go s.router.PushQuery(s.ctx.NodeID, s.ctx.ChainID, requestID, containerID, container) } - validatorList := validatorIDs.List() // Convert set to list for easier iteration - for _, validatorID := range validatorList { - vID := validatorID - s.timeouts.Register(validatorID, s.ctx.ChainID, requestID, func() { - s.router.QueryFailed(vID, s.ctx.ChainID, requestID) - }) - } + s.sender.PushQuery(validatorIDs, s.ctx.ChainID, requestID, containerID, container) } @@ -155,6 +162,14 @@ func (s *Sender) PushQuery(validatorIDs ids.ShortSet, requestID uint32, containe // their preferred frontier. func (s *Sender) PullQuery(validatorIDs ids.ShortSet, requestID uint32, containerID ids.ID) { s.ctx.Log.Verbo("Sending PullQuery. RequestID: %d. ContainerID: %s", requestID, containerID) + + for _, validatorID := range validatorIDs.List() { + vID := validatorID + s.timeouts.Register(validatorID, s.ctx.ChainID, requestID, func() { + s.router.QueryFailed(vID, s.ctx.ChainID, requestID) + }) + } + // If one of the validators in [validatorIDs] is myself, send this message directly // to my own router rather than sending it over the network if validatorIDs.Contains(s.ctx.NodeID) { // One of the validators in [validatorIDs] was myself @@ -164,13 +179,7 @@ func (s *Sender) PullQuery(validatorIDs ids.ShortSet, requestID uint32, containe // If this were not a goroutine, then we would deadlock when [handler].msgs is full go s.router.PullQuery(s.ctx.NodeID, s.ctx.ChainID, requestID, containerID) } - validatorList := validatorIDs.List() // Convert set to list for easier iteration - for _, validatorID := range validatorList { - vID := validatorID - s.timeouts.Register(validatorID, s.ctx.ChainID, requestID, func() { - s.router.QueryFailed(vID, s.ctx.ChainID, requestID) - }) - } + s.sender.PullQuery(validatorIDs, s.ctx.ChainID, requestID, containerID) } @@ -181,9 +190,9 @@ func (s *Sender) Chits(validatorID ids.ShortID, requestID uint32, votes ids.Set) // to my own router rather than sending it over the network if validatorID.Equals(s.ctx.NodeID) { go s.router.Chits(validatorID, s.ctx.ChainID, requestID, votes) - return + } else { + s.sender.Chits(validatorID, s.ctx.ChainID, requestID, votes) } - s.sender.Chits(validatorID, s.ctx.ChainID, requestID, votes) } // Gossip the provided container diff --git a/snow/networking/sender/sender_test.go b/snow/networking/sender/sender_test.go index 7760307..8be5e99 100644 --- a/snow/networking/sender/sender_test.go +++ b/snow/networking/sender/sender_test.go @@ -4,18 +4,20 @@ package sender import ( + "math/rand" "reflect" "sync" "testing" "time" + "github.com/prometheus/client_golang/prometheus" + "github.com/ava-labs/gecko/ids" "github.com/ava-labs/gecko/snow" "github.com/ava-labs/gecko/snow/engine/common" "github.com/ava-labs/gecko/snow/networking/router" "github.com/ava-labs/gecko/snow/networking/timeout" "github.com/ava-labs/gecko/utils/logging" - "github.com/prometheus/client_golang/prometheus" ) func TestSenderContext(t *testing.T) { @@ -82,3 +84,128 @@ func TestTimeout(t *testing.T) { t.Fatalf("Timeouts should have fired") } } + +func TestReliableMessages(t *testing.T) { + tm := timeout.Manager{} + tm.Initialize(50 * time.Millisecond) + go tm.Dispatch() + + chainRouter := router.ChainRouter{} + chainRouter.Initialize(logging.NoLog{}, &tm, time.Hour, time.Second) + + sender := Sender{} + sender.Initialize(snow.DefaultContextTest(), &ExternalSenderTest{}, &chainRouter, &tm) + + engine := common.EngineTest{T: t} + engine.Default(true) + + engine.ContextF = snow.DefaultContextTest + engine.GossipF = func() error { return nil } + + queriesToSend := 1000 + awaiting := make([]chan struct{}, queriesToSend) + for i := 0; i < queriesToSend; i++ { + awaiting[i] = make(chan struct{}, 1) + } + + engine.QueryFailedF = func(validatorID ids.ShortID, reqID uint32) error { + close(awaiting[int(reqID)]) + return nil + } + + handler := router.Handler{} + handler.Initialize( + &engine, + nil, + 1, + "", + prometheus.NewRegistry(), + ) + go handler.Dispatch() + + chainRouter.AddChain(&handler) + + go func() { + for i := 0; i < queriesToSend; i++ { + vdrIDs := ids.ShortSet{} + vdrIDs.Add(ids.NewShortID([20]byte{1})) + + sender.PullQuery(vdrIDs, uint32(i), ids.Empty) + time.Sleep(time.Duration(rand.Float64() * float64(time.Microsecond))) + } + }() + + go func() { + for { + chainRouter.Gossip() + time.Sleep(time.Duration(rand.Float64() * float64(time.Microsecond))) + } + }() + + for _, await := range awaiting { + _, _ = <-await + } +} + +func TestReliableMessagesToMyself(t *testing.T) { + tm := timeout.Manager{} + tm.Initialize(50 * time.Millisecond) + go tm.Dispatch() + + chainRouter := router.ChainRouter{} + chainRouter.Initialize(logging.NoLog{}, &tm, time.Hour, time.Second) + + sender := Sender{} + sender.Initialize(snow.DefaultContextTest(), &ExternalSenderTest{}, &chainRouter, &tm) + + engine := common.EngineTest{T: t} + engine.Default(false) + + engine.ContextF = snow.DefaultContextTest + engine.GossipF = func() error { return nil } + engine.CantPullQuery = false + + queriesToSend := 2 + awaiting := make([]chan struct{}, queriesToSend) + for i := 0; i < queriesToSend; i++ { + awaiting[i] = make(chan struct{}, 1) + } + + engine.QueryFailedF = func(validatorID ids.ShortID, reqID uint32) error { + close(awaiting[int(reqID)]) + return nil + } + + handler := router.Handler{} + handler.Initialize( + &engine, + nil, + 1, + "", + prometheus.NewRegistry(), + ) + go handler.Dispatch() + + chainRouter.AddChain(&handler) + + go func() { + for i := 0; i < queriesToSend; i++ { + vdrIDs := ids.ShortSet{} + vdrIDs.Add(engine.Context().NodeID) + + sender.PullQuery(vdrIDs, uint32(i), ids.Empty) + time.Sleep(time.Duration(rand.Float64() * float64(time.Microsecond))) + } + }() + + go func() { + for { + chainRouter.Gossip() + time.Sleep(time.Duration(rand.Float64() * float64(time.Microsecond))) + } + }() + + for _, await := range awaiting { + _, _ = <-await + } +} diff --git a/snow/validators/set.go b/snow/validators/set.go index 50210bf..610a85f 100644 --- a/snow/validators/set.go +++ b/snow/validators/set.go @@ -13,6 +13,19 @@ import ( "github.com/ava-labs/gecko/utils/random" ) +const ( + // maxExcessCapacityFactor ... + // If, when the validator set is reset, cap(set)/len(set) > MaxExcessCapacityFactor, + // the underlying arrays' capacities will be reduced by a factor of capacityReductionFactor. + // Higher value for maxExcessCapacityFactor --> less aggressive array downsizing --> less memory allocations + // but more unnecessary data in the underlying array that can't be garbage collected. + // Higher value for capacityReductionFactor --> more aggressive array downsizing --> more memory allocations + // but less unnecessary data in the underlying array that can't be garbage collected. + maxExcessCapacityFactor = 4 + // CapacityReductionFactor ... + capacityReductionFactor = 2 +) + // Set of validators that can be sampled type Set interface { fmt.Stringer @@ -71,9 +84,21 @@ func (s *set) Set(vdrs []Validator) { } func (s *set) set(vdrs []Validator) { - s.vdrMap = make(map[[20]byte]int, len(vdrs)) - s.vdrSlice = s.vdrSlice[:0] - s.sampler.Weights = s.sampler.Weights[:0] + lenVdrs := len(vdrs) + // If the underlying arrays are much larger than necessary, resize them to + // allow garbage collection of unused memory + if cap(s.vdrSlice) > len(s.vdrSlice)*maxExcessCapacityFactor { + newCap := cap(s.vdrSlice) / capacityReductionFactor + if newCap < lenVdrs { + newCap = lenVdrs + } + s.vdrSlice = make([]Validator, 0, newCap) + s.sampler.Weights = make([]uint64, 0, newCap) + } else { + s.vdrSlice = s.vdrSlice[:0] + s.sampler.Weights = s.sampler.Weights[:0] + } + s.vdrMap = make(map[[20]byte]int, lenVdrs) for _, vdr := range vdrs { s.add(vdr) diff --git a/staking/gen_staker_key.go b/staking/gen_staker_key.go index 8969ea3..37142c4 100644 --- a/staking/gen_staker_key.go +++ b/staking/gen_staker_key.go @@ -40,20 +40,27 @@ func GenerateStakingKeyCert(keyPath, certPath string) error { return fmt.Errorf("couldn't create certificate: %w", err) } - // Write cert to disk - if err := os.MkdirAll(filepath.Dir(certPath), 0755); err != nil { - return fmt.Errorf("couldn't create path for key/cert: %w", err) + // Ensure directory where key/cert will live exist + if err := os.MkdirAll(filepath.Dir(certPath), 0700); err != nil { + return fmt.Errorf("couldn't create path for cert: %w", err) + } else if err := os.MkdirAll(filepath.Dir(keyPath), 0700); err != nil { + return fmt.Errorf("couldn't create path for key: %w", err) } - certOut, err := os.Create(certPath) + + // Write cert to disk + certFile, err := os.Create(certPath) if err != nil { return fmt.Errorf("couldn't create cert file: %w", err) } - if err := pem.Encode(certOut, &pem.Block{Type: "CERTIFICATE", Bytes: certBytes}); err != nil { + if err := pem.Encode(certFile, &pem.Block{Type: "CERTIFICATE", Bytes: certBytes}); err != nil { return fmt.Errorf("couldn't write cert file: %w", err) } - if err := certOut.Close(); err != nil { + if err := certFile.Close(); err != nil { return fmt.Errorf("couldn't close cert file: %w", err) } + if err := os.Chmod(certPath, 0400); err != nil { // Make cert read-only + return fmt.Errorf("couldn't change permissions on cert: %w", err) + } // Write key to disk keyOut, err := os.Create(keyPath) @@ -70,5 +77,9 @@ func GenerateStakingKeyCert(keyPath, certPath string) error { if err := keyOut.Close(); err != nil { return fmt.Errorf("couldn't close key file: %w", err) } + if err := os.Chmod(keyPath, 0400); err != nil { // Make key read-only + return fmt.Errorf("couldn't change permissions on key") + } + return nil } diff --git a/utils/codec/codec.go b/utils/codec/codec.go new file mode 100644 index 0000000..6521993 --- /dev/null +++ b/utils/codec/codec.go @@ -0,0 +1,394 @@ +// (c) 2019-2020, Ava Labs, Inc. All rights reserved. +// See the file LICENSE for licensing terms. + +package codec + +import ( + "errors" + "fmt" + "reflect" + "unicode" + + "github.com/ava-labs/gecko/utils/wrappers" +) + +const ( + defaultMaxSize = 1 << 18 // default max size, in bytes, of something being marshalled by Marshal() + defaultMaxSliceLength = 1 << 18 // default max length of a slice being marshalled by Marshal(). Should be <= math.MaxUint32. + + // initial capacity of byte slice that values are marshaled into. + // Larger value --> need less memory allocations but possibly have allocated but unused memory + // Smaller value --> need more memory allocations but more efficient use of allocated memory + initialSliceCap = 256 +) + +var ( + errNil = errors.New("can't marshal/unmarshal nil pointer or interface") + errNeedPointer = errors.New("argument to unmarshal should be a pointer") +) + +// Codec handles marshaling and unmarshaling of structs +type codec struct { + maxSize int + maxSliceLen int + + typeIDToType map[uint32]reflect.Type + typeToTypeID map[reflect.Type]uint32 + + // Key: a struct type + // Value: Slice where each element is index in the struct type + // of a field that is serialized/deserialized + // e.g. Foo --> [1,5,8] means Foo.Field(1), etc. are to be serialized/deserialized + // We assume this cache is pretty small (a few hundred keys at most) + // and doesn't take up much memory + serializedFieldIndices map[reflect.Type][]int +} + +// Codec marshals and unmarshals +type Codec interface { + RegisterType(interface{}) error + Marshal(interface{}) ([]byte, error) + Unmarshal([]byte, interface{}) error +} + +// New returns a new codec +func New(maxSize, maxSliceLen int) Codec { + return &codec{ + maxSize: maxSize, + maxSliceLen: maxSliceLen, + typeIDToType: map[uint32]reflect.Type{}, + typeToTypeID: map[reflect.Type]uint32{}, + serializedFieldIndices: map[reflect.Type][]int{}, + } +} + +// NewDefault returns a new codec with reasonable default values +func NewDefault() Codec { return New(defaultMaxSize, defaultMaxSliceLength) } + +// RegisterType is used to register types that may be unmarshaled into an interface +// [val] is a value of the type being registered +func (c *codec) RegisterType(val interface{}) error { + valType := reflect.TypeOf(val) + if _, exists := c.typeToTypeID[valType]; exists { + return fmt.Errorf("type %v has already been registered", valType) + } + c.typeIDToType[uint32(len(c.typeIDToType))] = reflect.TypeOf(val) + c.typeToTypeID[valType] = uint32(len(c.typeIDToType) - 1) + return nil +} + +// A few notes: +// 1) See codec_test.go for examples of usage +// 2) We use "marshal" and "serialize" interchangeably, and "unmarshal" and "deserialize" interchangeably +// 3) To include a field of a struct in the serialized form, add the tag `serialize:"true"` to it +// 4) These typed members of a struct may be serialized: +// bool, string, uint[8,16,32,64], int[8,16,32,64], +// structs, slices, arrays, interface. +// structs, slices and arrays can only be serialized if their constituent values can be. +// 5) To marshal an interface, you must pass a pointer to the value +// 6) To unmarshal an interface, you must call codec.RegisterType([instance of the type that fulfills the interface]). +// 7) Serialized fields must be exported +// 8) nil slices are marshaled as empty slices + +// To marshal an interface, [value] must be a pointer to the interface +func (c *codec) Marshal(value interface{}) ([]byte, error) { + if value == nil { + return nil, errNil // can't marshal nil + } + + p := &wrappers.Packer{MaxSize: c.maxSize, Bytes: make([]byte, 0, initialSliceCap)} + if err := c.marshal(reflect.ValueOf(value), p); err != nil { + return nil, err + } + + return p.Bytes, nil +} + +// marshal writes the byte representation of [value] to [p] +// [value]'s underlying value must not be a nil pointer or interface +func (c *codec) marshal(value reflect.Value, p *wrappers.Packer) error { + valueKind := value.Kind() + switch valueKind { + case reflect.Interface, reflect.Ptr, reflect.Invalid: + if value.IsNil() { // Can't marshal nil (except nil slices) + return errNil + } + } + + switch valueKind { + case reflect.Uint8: + p.PackByte(uint8(value.Uint())) + return p.Err + case reflect.Int8: + p.PackByte(uint8(value.Int())) + return p.Err + case reflect.Uint16: + p.PackShort(uint16(value.Uint())) + return p.Err + case reflect.Int16: + p.PackShort(uint16(value.Int())) + return p.Err + case reflect.Uint32: + p.PackInt(uint32(value.Uint())) + return p.Err + case reflect.Int32: + p.PackInt(uint32(value.Int())) + return p.Err + case reflect.Uint64: + p.PackLong(value.Uint()) + return p.Err + case reflect.Int64: + p.PackLong(uint64(value.Int())) + return p.Err + case reflect.String: + p.PackStr(value.String()) + return p.Err + case reflect.Bool: + p.PackBool(value.Bool()) + return p.Err + case reflect.Uintptr, reflect.Ptr: + return c.marshal(value.Elem(), p) + case reflect.Interface: + underlyingValue := value.Interface() + typeID, ok := c.typeToTypeID[reflect.TypeOf(underlyingValue)] // Get the type ID of the value being marshaled + if !ok { + return fmt.Errorf("can't marshal unregistered type '%v'", reflect.TypeOf(underlyingValue).String()) + } + p.PackInt(typeID) // Pack type ID so we know what to unmarshal this into + if p.Err != nil { + return p.Err + } + if err := c.marshal(value.Elem(), p); err != nil { + return err + } + return p.Err + case reflect.Slice: + numElts := value.Len() // # elements in the slice/array. 0 if this slice is nil. + if numElts > c.maxSliceLen { + return fmt.Errorf("slice length, %d, exceeds maximum length, %d", numElts, c.maxSliceLen) + } + p.PackInt(uint32(numElts)) // pack # elements + if p.Err != nil { + return p.Err + } + for i := 0; i < numElts; i++ { // Process each element in the slice + if err := c.marshal(value.Index(i), p); err != nil { + return err + } + } + return nil + case reflect.Array: + numElts := value.Len() + if numElts > c.maxSliceLen { + return fmt.Errorf("array length, %d, exceeds maximum length, %d", numElts, c.maxSliceLen) + } + for i := 0; i < numElts; i++ { // Process each element in the array + if err := c.marshal(value.Index(i), p); err != nil { + return err + } + } + return nil + case reflect.Struct: + serializedFields, err := c.getSerializedFieldIndices(value.Type()) + if err != nil { + return err + } + for _, fieldIndex := range serializedFields { // Go through all fields of this struct that are serialized + if err := c.marshal(value.Field(fieldIndex), p); err != nil { // Serialize the field and write to byte array + return err + } + } + return nil + default: + return fmt.Errorf("can't marshal unknown kind %s", valueKind) + } +} + +// Unmarshal unmarshals [bytes] into [dest], where +// [dest] must be a pointer or interface +func (c *codec) Unmarshal(bytes []byte, dest interface{}) error { + switch { + case len(bytes) > c.maxSize: + return fmt.Errorf("byte array exceeds maximum length, %d", c.maxSize) + case dest == nil: + return errNil + } + + destPtr := reflect.ValueOf(dest) + if destPtr.Kind() != reflect.Ptr { + return errNeedPointer + } + + p := &wrappers.Packer{MaxSize: c.maxSize, Bytes: bytes} + destVal := destPtr.Elem() + if err := c.unmarshal(p, destVal); err != nil { + return err + } + + return nil +} + +// Unmarshal from p.Bytes into [value]. [value] must be addressable. +func (c *codec) unmarshal(p *wrappers.Packer, value reflect.Value) error { + switch value.Kind() { + case reflect.Uint8: + value.SetUint(uint64(p.UnpackByte())) + if p.Err != nil { + return fmt.Errorf("couldn't unmarshal uint8: %s", p.Err) + } + return nil + case reflect.Int8: + value.SetInt(int64(p.UnpackByte())) + if p.Err != nil { + return fmt.Errorf("couldn't unmarshal int8: %s", p.Err) + } + return nil + case reflect.Uint16: + value.SetUint(uint64(p.UnpackShort())) + if p.Err != nil { + return fmt.Errorf("couldn't unmarshal uint16: %s", p.Err) + } + return nil + case reflect.Int16: + value.SetInt(int64(p.UnpackShort())) + if p.Err != nil { + return fmt.Errorf("couldn't unmarshal int16: %s", p.Err) + } + return nil + case reflect.Uint32: + value.SetUint(uint64(p.UnpackInt())) + if p.Err != nil { + return fmt.Errorf("couldn't unmarshal uint32: %s", p.Err) + } + return nil + case reflect.Int32: + value.SetInt(int64(p.UnpackInt())) + if p.Err != nil { + return fmt.Errorf("couldn't unmarshal int32: %s", p.Err) + } + return nil + case reflect.Uint64: + value.SetUint(uint64(p.UnpackLong())) + if p.Err != nil { + return fmt.Errorf("couldn't unmarshal uint64: %s", p.Err) + } + return nil + case reflect.Int64: + value.SetInt(int64(p.UnpackLong())) + if p.Err != nil { + return fmt.Errorf("couldn't unmarshal int64: %s", p.Err) + } + return nil + case reflect.Bool: + value.SetBool(p.UnpackBool()) + if p.Err != nil { + return fmt.Errorf("couldn't unmarshal bool: %s", p.Err) + } + return nil + case reflect.Slice: + numElts := int(p.UnpackInt()) + if p.Err != nil { + return fmt.Errorf("couldn't unmarshal slice: %s", p.Err) + } + // set [value] to be a slice of the appropriate type/capacity (right now it is nil) + value.Set(reflect.MakeSlice(value.Type(), numElts, numElts)) + // Unmarshal each element into the appropriate index of the slice + for i := 0; i < numElts; i++ { + if err := c.unmarshal(p, value.Index(i)); err != nil { + return fmt.Errorf("couldn't unmarshal slice element: %s", err) + } + } + return nil + case reflect.Array: + for i := 0; i < value.Len(); i++ { + if err := c.unmarshal(p, value.Index(i)); err != nil { + return fmt.Errorf("couldn't unmarshal array element: %s", err) + } + } + return nil + case reflect.String: + value.SetString(p.UnpackStr()) + if p.Err != nil { + return fmt.Errorf("couldn't unmarshal string: %s", p.Err) + } + return nil + case reflect.Interface: + typeID := p.UnpackInt() // Get the type ID + if p.Err != nil { + return fmt.Errorf("couldn't unmarshal interface: %s", p.Err) + } + // Get a type that implements the interface + implementingType, ok := c.typeIDToType[typeID] + if !ok { + return fmt.Errorf("couldn't unmarshal interface: unknown type ID %d", typeID) + } + // Ensure type actually does implement the interface + if valueType := value.Type(); !implementingType.Implements(valueType) { + return fmt.Errorf("couldn't unmarshal interface: %s does not implement interface %s", implementingType, valueType) + } + intfImplementor := reflect.New(implementingType).Elem() // instance of the proper type + // Unmarshal into the struct + if err := c.unmarshal(p, intfImplementor); err != nil { + return fmt.Errorf("couldn't unmarshal interface: %s", err) + } + // And assign the filled struct to the value + value.Set(intfImplementor) + return nil + case reflect.Struct: + // Get indices of fields that will be unmarshaled into + serializedFieldIndices, err := c.getSerializedFieldIndices(value.Type()) + if err != nil { + return fmt.Errorf("couldn't unmarshal struct: %s", err) + } + // Go through the fields and umarshal into them + for _, index := range serializedFieldIndices { + if err := c.unmarshal(p, value.Field(index)); err != nil { + return fmt.Errorf("couldn't unmarshal struct: %s", err) + } + } + return nil + case reflect.Ptr: + // Get the type this pointer points to + t := value.Type().Elem() + // Create a new pointer to a new value of the underlying type + v := reflect.New(t) + // Fill the value + if err := c.unmarshal(p, v.Elem()); err != nil { + return fmt.Errorf("couldn't unmarshal pointer: %s", err) + } + // Assign to the top-level struct's member + value.Set(v) + return nil + case reflect.Invalid: + return errNil + default: + return fmt.Errorf("can't unmarshal unknown type %s", value.Kind().String()) + } +} + +// Returns the indices of the serializable fields of [t], which is a struct type +// Returns an error if a field has tag "serialize: true" but the field is unexported +// e.g. getSerializedFieldIndices(Foo) --> [1,5,8] means Foo.Field(1), Foo.Field(5), Foo.Field(8) +// are to be serialized/deserialized +func (c *codec) getSerializedFieldIndices(t reflect.Type) ([]int, error) { + if c.serializedFieldIndices == nil { + c.serializedFieldIndices = make(map[reflect.Type][]int) + } + if serializedFields, ok := c.serializedFieldIndices[t]; ok { // use pre-computed result + return serializedFields, nil + } + numFields := t.NumField() + serializedFields := make([]int, 0, numFields) + for i := 0; i < numFields; i++ { // Go through all fields of this struct + field := t.Field(i) + if field.Tag.Get("serialize") != "true" { // Skip fields we don't need to serialize + continue + } + if unicode.IsLower(rune(field.Name[0])) { // Can only marshal exported fields + return []int{}, fmt.Errorf("can't marshal unexported field %s", field.Name) + } + serializedFields = append(serializedFields, i) + } + c.serializedFieldIndices[t] = serializedFields // cache result + return serializedFields, nil +} diff --git a/vms/components/codec/codec_benchmark_test.go b/utils/codec/codec_benchmark_test.go similarity index 84% rename from vms/components/codec/codec_benchmark_test.go rename to utils/codec/codec_benchmark_test.go index 8e6f9f7..4adfa52 100644 --- a/vms/components/codec/codec_benchmark_test.go +++ b/utils/codec/codec_benchmark_test.go @@ -35,13 +35,22 @@ func BenchmarkMarshal(b *testing.B) { }, MyPointer: &temp, } + var unmarshaledMyStructInstance myStruct codec := NewDefault() codec.RegisterType(&MyInnerStruct{}) // Register the types that may be unmarshaled into interfaces codec.RegisterType(&MyInnerStruct2{}) + codec.Marshal(myStructInstance) // warm up serializedFields cache b.ResetTimer() for n := 0; n < b.N; n++ { - codec.Marshal(myStructInstance) + bytes, err := codec.Marshal(myStructInstance) + if err != nil { + b.Fatal(err) + } + if err := codec.Unmarshal(bytes, &unmarshaledMyStructInstance); err != nil { + b.Fatal(err) + } + } } diff --git a/vms/components/codec/codec_test.go b/utils/codec/codec_test.go similarity index 80% rename from vms/components/codec/codec_test.go rename to utils/codec/codec_test.go index 6fdfeba..edd3f85 100644 --- a/vms/components/codec/codec_test.go +++ b/utils/codec/codec_test.go @@ -5,6 +5,7 @@ package codec import ( "bytes" + "math" "reflect" "testing" ) @@ -104,36 +105,8 @@ func TestStruct(t *testing.T) { t.Fatal(err) } - if !reflect.DeepEqual(myStructUnmarshaled.Member1, myStructInstance.Member1) { - t.Fatal("expected unmarshaled struct to be same as original struct") - } else if !bytes.Equal(myStructUnmarshaled.MySlice, myStructInstance.MySlice) { - t.Fatal("expected unmarshaled struct to be same as original struct") - } else if !reflect.DeepEqual(myStructUnmarshaled.MySlice2, myStructInstance.MySlice2) { - t.Fatal("expected unmarshaled struct to be same as original struct") - } else if !reflect.DeepEqual(myStructUnmarshaled.MySlice3, myStructInstance.MySlice3) { - t.Fatal("expected unmarshaled struct to be same as original struct") - } else if !reflect.DeepEqual(myStructUnmarshaled.MySlice3, myStructInstance.MySlice3) { - t.Fatal("expected unmarshaled struct to be same as original struct") - } else if !reflect.DeepEqual(myStructUnmarshaled.MySlice4, myStructInstance.MySlice4) { - t.Fatal("expected unmarshaled struct to be same as original struct") - } else if !reflect.DeepEqual(myStructUnmarshaled.InnerStruct, myStructInstance.InnerStruct) { - t.Fatal("expected unmarshaled struct to be same as original struct") - } else if !reflect.DeepEqual(myStructUnmarshaled.InnerStruct2, myStructInstance.InnerStruct2) { - t.Fatal("expected unmarshaled struct to be same as original struct") - } else if !reflect.DeepEqual(myStructUnmarshaled.MyArray2, myStructInstance.MyArray2) { - t.Fatal("expected unmarshaled struct to be same as original struct") - } else if !reflect.DeepEqual(myStructUnmarshaled.MyArray3, myStructInstance.MyArray3) { - t.Fatal("expected unmarshaled struct to be same as original struct") - } else if !reflect.DeepEqual(myStructUnmarshaled.MyArray4, myStructInstance.MyArray4) { - t.Fatal("expected unmarshaled struct to be same as original struct") - } else if !reflect.DeepEqual(myStructUnmarshaled.MyInterface, myStructInstance.MyInterface) { - t.Fatal("expected unmarshaled struct to be same as original struct") - } else if !reflect.DeepEqual(myStructUnmarshaled.MySlice5, myStructInstance.MySlice5) { - t.Fatal("expected unmarshaled struct to be same as original struct") - } else if !reflect.DeepEqual(myStructUnmarshaled.InnerStruct3, myStructInstance.InnerStruct3) { - t.Fatal("expected unmarshaled struct to be same as original struct") - } else if !reflect.DeepEqual(myStructUnmarshaled.MyPointer, myStructInstance.MyPointer) { - t.Fatal("expected unmarshaled struct to be same as original struct") + if !reflect.DeepEqual(*myStructUnmarshaled, myStructInstance) { + t.Fatal("should be same") } } @@ -173,6 +146,28 @@ func TestSlice(t *testing.T) { } } +// Test marshalling/unmarshalling largest possible slice +func TestMaxSizeSlice(t *testing.T) { + mySlice := make([]string, math.MaxUint16, math.MaxUint16) + mySlice[0] = "first!" + mySlice[math.MaxUint16-1] = "last!" + codec := NewDefault() + bytes, err := codec.Marshal(mySlice) + if err != nil { + t.Fatal(err) + } + + var sliceUnmarshaled []string + if err := codec.Unmarshal(bytes, &sliceUnmarshaled); err != nil { + t.Fatal(err) + } + + if !reflect.DeepEqual(mySlice, sliceUnmarshaled) { + t.Fatal("expected marshaled and unmarshaled values to match") + } +} + +// Test marshalling a bool func TestBool(t *testing.T) { myBool := true codec := NewDefault() @@ -191,6 +186,7 @@ func TestBool(t *testing.T) { } } +// Test marshalling an array func TestArray(t *testing.T) { myArr := [5]uint64{5, 6, 7, 8, 9} codec := NewDefault() @@ -209,6 +205,26 @@ func TestArray(t *testing.T) { } } +// Test marshalling a really big array +func TestBigArray(t *testing.T) { + myArr := [30000]uint64{5, 6, 7, 8, 9} + codec := NewDefault() + bytes, err := codec.Marshal(myArr) + if err != nil { + t.Fatal(err) + } + + var myArrUnmarshaled [30000]uint64 + if err := codec.Unmarshal(bytes, &myArrUnmarshaled); err != nil { + t.Fatal(err) + } + + if !reflect.DeepEqual(myArr, myArrUnmarshaled) { + t.Fatal("expected marshaled and unmarshaled values to match") + } +} + +// Test marshalling a pointer to a struct func TestPointerToStruct(t *testing.T) { myPtr := &MyInnerStruct{Str: "Hello!"} codec := NewDefault() @@ -227,6 +243,7 @@ func TestPointerToStruct(t *testing.T) { } } +// Test marshalling a slice of structs func TestSliceOfStruct(t *testing.T) { mySlice := []MyInnerStruct3{ MyInnerStruct3{ @@ -257,6 +274,7 @@ func TestSliceOfStruct(t *testing.T) { } } +// Test marshalling an interface func TestInterface(t *testing.T) { codec := NewDefault() codec.RegisterType(&MyInnerStruct2{}) @@ -278,6 +296,7 @@ func TestInterface(t *testing.T) { } } +// Test marshalling a slice of interfaces func TestSliceOfInterface(t *testing.T) { mySlice := []Foo{ &MyInnerStruct{ @@ -304,6 +323,7 @@ func TestSliceOfInterface(t *testing.T) { } } +// Test marshalling an array of interfaces func TestArrayOfInterface(t *testing.T) { myArray := [2]Foo{ &MyInnerStruct{ @@ -330,6 +350,7 @@ func TestArrayOfInterface(t *testing.T) { } } +// Test marshalling a pointer to an interface func TestPointerToInterface(t *testing.T) { var myinnerStruct Foo = &MyInnerStruct{Str: "Hello!"} var myPtr *Foo = &myinnerStruct @@ -352,6 +373,7 @@ func TestPointerToInterface(t *testing.T) { } } +// Test marshalling a string func TestString(t *testing.T) { myString := "Ayy" codec := NewDefault() @@ -370,7 +392,7 @@ func TestString(t *testing.T) { } } -// Ensure a nil slice is unmarshaled as an empty slice +// Ensure a nil slice is unmarshaled to slice with length 0 func TestNilSlice(t *testing.T) { type structWithSlice struct { Slice []byte `serialize:"true"` @@ -389,12 +411,12 @@ func TestNilSlice(t *testing.T) { } if structUnmarshaled.Slice == nil || len(structUnmarshaled.Slice) != 0 { - t.Fatal("expected slice to be empty slice") + t.Fatal("expected slice to be non-nil and length 0") } } // Ensure that trying to serialize a struct with an unexported member -// that has `serialize:"true"` returns errUnexportedField +// that has `serialize:"true"` returns error func TestSerializeUnexportedField(t *testing.T) { type s struct { ExportedField string `serialize:"true"` @@ -407,8 +429,8 @@ func TestSerializeUnexportedField(t *testing.T) { } codec := NewDefault() - if _, err := codec.Marshal(myS); err != errMarshalUnexportedField { - t.Fatalf("expected err to be errUnexportedField but was %v", err) + if _, err := codec.Marshal(myS); err == nil { + t.Fatalf("expected err but got none") } } @@ -426,12 +448,12 @@ func TestSerializeOfNoSerializeField(t *testing.T) { codec := NewDefault() marshalled, err := codec.Marshal(myS) if err != nil { - t.Fatalf("Unexpected error %q", err) + t.Fatal(err) } unmarshalled := s{} err = codec.Unmarshal(marshalled, &unmarshalled) if err != nil { - t.Fatalf("Unexpected error %q", err) + t.Fatal(err) } expectedUnmarshalled := s{SerializedField: "Serialize me"} if !reflect.DeepEqual(unmarshalled, expectedUnmarshalled) { @@ -443,11 +465,12 @@ type simpleSliceStruct struct { Arr []uint32 `serialize:"true"` } -func TestEmptySliceSerialization(t *testing.T) { +// Test marshalling of nil slice +func TestNilSliceSerialization(t *testing.T) { codec := NewDefault() val := &simpleSliceStruct{} - expected := []byte{0, 0, 0, 0} + expected := []byte{0, 0, 0, 0} // nil slice marshaled as 0 length slice result, err := codec.Marshal(val) if err != nil { t.Fatal(err) @@ -456,6 +479,36 @@ func TestEmptySliceSerialization(t *testing.T) { if !bytes.Equal(expected, result) { t.Fatalf("\nExpected: 0x%x\nResult: 0x%x", expected, result) } + + valUnmarshaled := &simpleSliceStruct{} + if err = codec.Unmarshal(result, &valUnmarshaled); err != nil { + t.Fatal(err) + } else if len(valUnmarshaled.Arr) != 0 { + t.Fatal("should be 0 length") + } +} + +// Test marshaling a slice that has 0 elements (but isn't nil) +func TestEmptySliceSerialization(t *testing.T) { + codec := NewDefault() + + val := &simpleSliceStruct{Arr: make([]uint32, 0, 1)} + expected := []byte{0, 0, 0, 0} // 0 for size + result, err := codec.Marshal(val) + if err != nil { + t.Fatal(err) + } + + if !bytes.Equal(expected, result) { + t.Fatalf("\nExpected: 0x%x\nResult: 0x%x", expected, result) + } + + valUnmarshaled := &simpleSliceStruct{} + if err = codec.Unmarshal(result, &valUnmarshaled); err != nil { + t.Fatal(err) + } else if !reflect.DeepEqual(valUnmarshaled, val) { + t.Fatal("should be same") + } } type emptyStruct struct{} @@ -464,13 +517,14 @@ type nestedSliceStruct struct { Arr []emptyStruct `serialize:"true"` } +// Test marshaling slice that is not nil and not empty func TestSliceWithEmptySerialization(t *testing.T) { codec := NewDefault() val := &nestedSliceStruct{ Arr: make([]emptyStruct, 1000), } - expected := []byte{0x00, 0x00, 0x03, 0xE8} + expected := []byte{0x00, 0x00, 0x03, 0xE8} //1000 for numElts result, err := codec.Marshal(val) if err != nil { t.Fatal(err) @@ -485,7 +539,7 @@ func TestSliceWithEmptySerialization(t *testing.T) { t.Fatal(err) } if len(unmarshaled.Arr) != 1000 { - t.Fatalf("Should have created an array of length %d", 1000) + t.Fatalf("Should have created a slice of length %d", 1000) } } @@ -493,20 +547,15 @@ func TestSliceWithEmptySerializationOutOfMemory(t *testing.T) { codec := NewDefault() val := &nestedSliceStruct{ - Arr: make([]emptyStruct, 1000000), + Arr: make([]emptyStruct, defaultMaxSliceLength+1), } - expected := []byte{0x00, 0x0f, 0x42, 0x40} // 1,000,000 in hex - result, err := codec.Marshal(val) - if err != nil { - t.Fatal(err) - } - - if !bytes.Equal(expected, result) { - t.Fatalf("\nExpected: 0x%x\nResult: 0x%x", expected, result) + bytes, err := codec.Marshal(val) + if err == nil { + t.Fatal("should have failed due to slice length too large") } unmarshaled := nestedSliceStruct{} - if err := codec.Unmarshal(expected, &unmarshaled); err == nil { + if err := codec.Unmarshal(bytes, &unmarshaled); err == nil { t.Fatalf("Should have errored due to excess memory requested") } } diff --git a/utils/wrappers/packing.go b/utils/wrappers/packing.go index 1038852..c048f9c 100644 --- a/utils/wrappers/packing.go +++ b/utils/wrappers/packing.go @@ -61,26 +61,23 @@ func (p *Packer) CheckSpace(bytes int) { } } -// Expand ensures that there is [bytes] bytes left of space in the byte array. -// If this is not allowed due to the maximum size, an error is added to the -// packer +// Expand ensures that there is [bytes] bytes left of space in the byte slice. +// If this is not allowed due to the maximum size, an error is added to the packer +// In order to understand this code, its important to understand the difference +// between a slice's length and its capacity. func (p *Packer) Expand(bytes int) { - p.CheckSpace(0) - if p.Errored() { + neededSize := bytes + p.Offset // Need byte slice's length to be at least [neededSize] + switch { + case neededSize <= len(p.Bytes): // Byte slice has sufficient length already return - } - - neededSize := bytes + p.Offset - if neededSize <= len(p.Bytes) { + case neededSize > p.MaxSize: // Lengthening the byte slice would cause it to grow too large + p.Err = errBadLength return - } - - if neededSize > p.MaxSize { - p.Add(errBadLength) - } else if neededSize > cap(p.Bytes) { - p.Bytes = append(p.Bytes[:cap(p.Bytes)], make([]byte, neededSize-cap(p.Bytes))...) - } else { + case neededSize <= cap(p.Bytes): // Byte slice has sufficient capacity to lengthen it without mem alloc p.Bytes = p.Bytes[:neededSize] + return + default: // Add capacity/length to byte slice + p.Bytes = append(p.Bytes[:cap(p.Bytes)], make([]byte, neededSize-cap(p.Bytes))...) } } diff --git a/vms/avm/base_tx.go b/vms/avm/base_tx.go index 33cba51..cf4371f 100644 --- a/vms/avm/base_tx.go +++ b/vms/avm/base_tx.go @@ -9,8 +9,8 @@ import ( "github.com/ava-labs/gecko/database" "github.com/ava-labs/gecko/ids" "github.com/ava-labs/gecko/snow" + "github.com/ava-labs/gecko/utils/codec" "github.com/ava-labs/gecko/vms/components/ava" - "github.com/ava-labs/gecko/vms/components/codec" "github.com/ava-labs/gecko/vms/components/verify" ) @@ -46,8 +46,8 @@ func (t *BaseTx) InputUTXOs() []*ava.UTXOID { return utxos } -// AssetIDs returns the IDs of the assets this transaction depends on -func (t *BaseTx) AssetIDs() ids.Set { +// ConsumedAssetIDs returns the IDs of the assets this transaction consumes +func (t *BaseTx) ConsumedAssetIDs() ids.Set { assets := ids.Set{} for _, in := range t.Ins { assets.Add(in.AssetID()) @@ -55,6 +55,11 @@ func (t *BaseTx) AssetIDs() ids.Set { return assets } +// AssetIDs returns the IDs of the assets this transaction depends on +func (t *BaseTx) AssetIDs() ids.Set { + return t.ConsumedAssetIDs() +} + // NumCredentials returns the number of expected credentials func (t *BaseTx) NumCredentials() int { return len(t.Ins) } diff --git a/vms/avm/base_tx_test.go b/vms/avm/base_tx_test.go index 163ef5c..9f7bbdd 100644 --- a/vms/avm/base_tx_test.go +++ b/vms/avm/base_tx_test.go @@ -77,7 +77,7 @@ func TestBaseTxSerialization(t *testing.T) { tx := &Tx{UnsignedTx: &BaseTx{ NetID: networkID, BCID: chainID, - Outs: []*ava.TransferableOutput{&ava.TransferableOutput{ + Outs: []*ava.TransferableOutput{{ Asset: ava.Asset{ID: asset}, Out: &secp256k1fx.TransferOutput{ Amt: 12345, @@ -87,7 +87,7 @@ func TestBaseTxSerialization(t *testing.T) { }, }, }}, - Ins: []*ava.TransferableInput{&ava.TransferableInput{ + Ins: []*ava.TransferableInput{{ UTXOID: ava.UTXOID{ TxID: ids.NewID([32]byte{ 0xff, 0xfe, 0xfd, 0xfc, 0xfb, 0xfa, 0xf9, 0xf8, @@ -125,7 +125,7 @@ func TestBaseTxGetters(t *testing.T) { tx := &BaseTx{ NetID: networkID, BCID: chainID, - Outs: []*ava.TransferableOutput{&ava.TransferableOutput{ + Outs: []*ava.TransferableOutput{{ Asset: ava.Asset{ID: asset}, Out: &secp256k1fx.TransferOutput{ Amt: 12345, @@ -135,7 +135,7 @@ func TestBaseTxGetters(t *testing.T) { }, }, }}, - Ins: []*ava.TransferableInput{&ava.TransferableInput{ + Ins: []*ava.TransferableInput{{ UTXOID: ava.UTXOID{ TxID: ids.NewID([32]byte{ 0xff, 0xfe, 0xfd, 0xfc, 0xfb, 0xfa, 0xf9, 0xf8, @@ -162,6 +162,10 @@ func TestBaseTxGetters(t *testing.T) { t.Fatalf("Wrong number of assets returned") } else if !assets.Contains(asset) { t.Fatalf("Wrong asset returned") + } else if assets := tx.ConsumedAssetIDs(); assets.Len() != 1 { + t.Fatalf("Wrong number of consumed assets returned") + } else if !assets.Contains(asset) { + t.Fatalf("Wrong consumed asset returned") } else if utxos := tx.UTXOs(); len(utxos) != 1 { t.Fatalf("Wrong number of utxos returned") } else if utxo := utxos[0]; !utxo.TxID.Equals(txID) { @@ -179,7 +183,7 @@ func TestBaseTxSyntacticVerify(t *testing.T) { tx := &BaseTx{ NetID: networkID, BCID: chainID, - Outs: []*ava.TransferableOutput{&ava.TransferableOutput{ + Outs: []*ava.TransferableOutput{{ Asset: ava.Asset{ID: asset}, Out: &secp256k1fx.TransferOutput{ Amt: 12345, @@ -189,7 +193,7 @@ func TestBaseTxSyntacticVerify(t *testing.T) { }, }, }}, - Ins: []*ava.TransferableInput{&ava.TransferableInput{ + Ins: []*ava.TransferableInput{{ UTXOID: ava.UTXOID{ TxID: ids.NewID([32]byte{ 0xff, 0xfe, 0xfd, 0xfc, 0xfb, 0xfa, 0xf9, 0xf8, @@ -230,7 +234,7 @@ func TestBaseTxSyntacticVerifyWrongNetworkID(t *testing.T) { tx := &BaseTx{ NetID: 0, BCID: chainID, - Outs: []*ava.TransferableOutput{&ava.TransferableOutput{ + Outs: []*ava.TransferableOutput{{ Asset: ava.Asset{ID: asset}, Out: &secp256k1fx.TransferOutput{ Amt: 12345, @@ -240,7 +244,7 @@ func TestBaseTxSyntacticVerifyWrongNetworkID(t *testing.T) { }, }, }}, - Ins: []*ava.TransferableInput{&ava.TransferableInput{ + Ins: []*ava.TransferableInput{{ UTXOID: ava.UTXOID{ TxID: ids.NewID([32]byte{ 0xff, 0xfe, 0xfd, 0xfc, 0xfb, 0xfa, 0xf9, 0xf8, @@ -272,7 +276,7 @@ func TestBaseTxSyntacticVerifyWrongChainID(t *testing.T) { tx := &BaseTx{ NetID: networkID, BCID: ids.Empty, - Outs: []*ava.TransferableOutput{&ava.TransferableOutput{ + Outs: []*ava.TransferableOutput{{ Asset: ava.Asset{ID: asset}, Out: &secp256k1fx.TransferOutput{ Amt: 12345, @@ -282,7 +286,7 @@ func TestBaseTxSyntacticVerifyWrongChainID(t *testing.T) { }, }, }}, - Ins: []*ava.TransferableInput{&ava.TransferableInput{ + Ins: []*ava.TransferableInput{{ UTXOID: ava.UTXOID{ TxID: ids.NewID([32]byte{ 0xff, 0xfe, 0xfd, 0xfc, 0xfb, 0xfa, 0xf9, 0xf8, @@ -315,7 +319,7 @@ func TestBaseTxSyntacticVerifyInvalidOutput(t *testing.T) { NetID: networkID, BCID: chainID, Outs: []*ava.TransferableOutput{nil}, - Ins: []*ava.TransferableInput{&ava.TransferableInput{ + Ins: []*ava.TransferableInput{{ UTXOID: ava.UTXOID{ TxID: ids.NewID([32]byte{ 0xff, 0xfe, 0xfd, 0xfc, 0xfb, 0xfa, 0xf9, 0xf8, @@ -348,7 +352,7 @@ func TestBaseTxSyntacticVerifyUnsortedOutputs(t *testing.T) { NetID: networkID, BCID: chainID, Outs: []*ava.TransferableOutput{ - &ava.TransferableOutput{ + { Asset: ava.Asset{ID: asset}, Out: &secp256k1fx.TransferOutput{ Amt: 2, @@ -358,7 +362,7 @@ func TestBaseTxSyntacticVerifyUnsortedOutputs(t *testing.T) { }, }, }, - &ava.TransferableOutput{ + { Asset: ava.Asset{ID: asset}, Out: &secp256k1fx.TransferOutput{ Amt: 1, @@ -370,7 +374,7 @@ func TestBaseTxSyntacticVerifyUnsortedOutputs(t *testing.T) { }, }, Ins: []*ava.TransferableInput{ - &ava.TransferableInput{ + { UTXOID: ava.UTXOID{ TxID: ids.NewID([32]byte{ 0xff, 0xfe, 0xfd, 0xfc, 0xfb, 0xfa, 0xf9, 0xf8, @@ -403,7 +407,7 @@ func TestBaseTxSyntacticVerifyInvalidInput(t *testing.T) { tx := &BaseTx{ NetID: networkID, BCID: chainID, - Outs: []*ava.TransferableOutput{&ava.TransferableOutput{ + Outs: []*ava.TransferableOutput{{ Asset: ava.Asset{ID: asset}, Out: &secp256k1fx.TransferOutput{ Amt: 12345, @@ -428,7 +432,7 @@ func TestBaseTxSyntacticVerifyInputOverflow(t *testing.T) { tx := &BaseTx{ NetID: networkID, BCID: chainID, - Outs: []*ava.TransferableOutput{&ava.TransferableOutput{ + Outs: []*ava.TransferableOutput{{ Asset: ava.Asset{ID: asset}, Out: &secp256k1fx.TransferOutput{ Amt: 12345, @@ -439,7 +443,7 @@ func TestBaseTxSyntacticVerifyInputOverflow(t *testing.T) { }, }}, Ins: []*ava.TransferableInput{ - &ava.TransferableInput{ + { UTXOID: ava.UTXOID{ TxID: ids.NewID([32]byte{ 0xff, 0xfe, 0xfd, 0xfc, 0xfb, 0xfa, 0xf9, 0xf8, @@ -457,7 +461,7 @@ func TestBaseTxSyntacticVerifyInputOverflow(t *testing.T) { }, }, }, - &ava.TransferableInput{ + { UTXOID: ava.UTXOID{ TxID: ids.NewID([32]byte{ 0xff, 0xfe, 0xfd, 0xfc, 0xfb, 0xfa, 0xf9, 0xf8, @@ -491,7 +495,7 @@ func TestBaseTxSyntacticVerifyOutputOverflow(t *testing.T) { NetID: networkID, BCID: chainID, Outs: []*ava.TransferableOutput{ - &ava.TransferableOutput{ + { Asset: ava.Asset{ID: asset}, Out: &secp256k1fx.TransferOutput{ Amt: 2, @@ -501,7 +505,7 @@ func TestBaseTxSyntacticVerifyOutputOverflow(t *testing.T) { }, }, }, - &ava.TransferableOutput{ + { Asset: ava.Asset{ID: asset}, Out: &secp256k1fx.TransferOutput{ Amt: math.MaxUint64, @@ -512,7 +516,7 @@ func TestBaseTxSyntacticVerifyOutputOverflow(t *testing.T) { }, }, }, - Ins: []*ava.TransferableInput{&ava.TransferableInput{ + Ins: []*ava.TransferableInput{{ UTXOID: ava.UTXOID{ TxID: ids.NewID([32]byte{ 0xff, 0xfe, 0xfd, 0xfc, 0xfb, 0xfa, 0xf9, 0xf8, @@ -544,7 +548,7 @@ func TestBaseTxSyntacticVerifyInsufficientFunds(t *testing.T) { tx := &BaseTx{ NetID: networkID, BCID: chainID, - Outs: []*ava.TransferableOutput{&ava.TransferableOutput{ + Outs: []*ava.TransferableOutput{{ Asset: ava.Asset{ID: asset}, Out: &secp256k1fx.TransferOutput{ Amt: math.MaxUint64, @@ -554,7 +558,7 @@ func TestBaseTxSyntacticVerifyInsufficientFunds(t *testing.T) { }, }, }}, - Ins: []*ava.TransferableInput{&ava.TransferableInput{ + Ins: []*ava.TransferableInput{{ UTXOID: ava.UTXOID{ TxID: ids.NewID([32]byte{ 0xff, 0xfe, 0xfd, 0xfc, 0xfb, 0xfa, 0xf9, 0xf8, @@ -586,7 +590,7 @@ func TestBaseTxSyntacticVerifyUninitialized(t *testing.T) { tx := &BaseTx{ NetID: networkID, BCID: chainID, - Outs: []*ava.TransferableOutput{&ava.TransferableOutput{ + Outs: []*ava.TransferableOutput{{ Asset: ava.Asset{ID: asset}, Out: &secp256k1fx.TransferOutput{ Amt: 12345, @@ -596,7 +600,7 @@ func TestBaseTxSyntacticVerifyUninitialized(t *testing.T) { }, }, }}, - Ins: []*ava.TransferableInput{&ava.TransferableInput{ + Ins: []*ava.TransferableInput{{ UTXOID: ava.UTXOID{ TxID: ids.NewID([32]byte{ 0xff, 0xfe, 0xfd, 0xfc, 0xfb, 0xfa, 0xf9, 0xf8, @@ -633,7 +637,7 @@ func TestBaseTxSemanticVerify(t *testing.T) { tx := &Tx{UnsignedTx: &BaseTx{ NetID: networkID, BCID: chainID, - Ins: []*ava.TransferableInput{&ava.TransferableInput{ + Ins: []*ava.TransferableInput{{ UTXOID: ava.UTXOID{ TxID: genesisTx.ID(), OutputIndex: 1, @@ -702,7 +706,7 @@ func TestBaseTxSemanticVerifyUnknownFx(t *testing.T) { tx := &Tx{UnsignedTx: &BaseTx{ NetID: networkID, BCID: chainID, - Ins: []*ava.TransferableInput{&ava.TransferableInput{ + Ins: []*ava.TransferableInput{{ UTXOID: ava.UTXOID{ TxID: genesisTx.ID(), OutputIndex: 1, @@ -754,7 +758,7 @@ func TestBaseTxSemanticVerifyWrongAssetID(t *testing.T) { tx := &Tx{UnsignedTx: &BaseTx{ NetID: networkID, BCID: chainID, - Ins: []*ava.TransferableInput{&ava.TransferableInput{ + Ins: []*ava.TransferableInput{{ UTXOID: ava.UTXOID{ TxID: genesisTx.ID(), OutputIndex: 1, @@ -825,11 +829,11 @@ func TestBaseTxSemanticVerifyUnauthorizedFx(t *testing.T) { genesisBytes, issuer, []*common.Fx{ - &common.Fx{ + { ID: ids.Empty, Fx: &secp256k1fx.Fx{}, }, - &common.Fx{ + { ID: ids.NewID([32]byte{1}), Fx: &testFx{}, }, @@ -863,7 +867,7 @@ func TestBaseTxSemanticVerifyUnauthorizedFx(t *testing.T) { tx := &Tx{UnsignedTx: &BaseTx{ NetID: networkID, BCID: chainID, - Ins: []*ava.TransferableInput{&ava.TransferableInput{ + Ins: []*ava.TransferableInput{{ UTXOID: ava.UTXOID{ TxID: genesisTx.ID(), OutputIndex: 1, @@ -923,7 +927,7 @@ func TestBaseTxSemanticVerifyInvalidSignature(t *testing.T) { tx := &Tx{UnsignedTx: &BaseTx{ NetID: networkID, BCID: chainID, - Ins: []*ava.TransferableInput{&ava.TransferableInput{ + Ins: []*ava.TransferableInput{{ UTXOID: ava.UTXOID{ TxID: genesisTx.ID(), OutputIndex: 1, @@ -941,9 +945,7 @@ func TestBaseTxSemanticVerifyInvalidSignature(t *testing.T) { }} tx.Creds = append(tx.Creds, &secp256k1fx.Credential{ - Sigs: [][crypto.SECP256K1RSigLen]byte{ - [crypto.SECP256K1RSigLen]byte{}, - }, + Sigs: [][crypto.SECP256K1RSigLen]byte{{}}, }) b, err := vm.codec.Marshal(tx) @@ -977,7 +979,7 @@ func TestBaseTxSemanticVerifyMissingUTXO(t *testing.T) { tx := &Tx{UnsignedTx: &BaseTx{ NetID: networkID, BCID: chainID, - Ins: []*ava.TransferableInput{&ava.TransferableInput{ + Ins: []*ava.TransferableInput{{ UTXOID: ava.UTXOID{ TxID: ids.Empty, OutputIndex: 1, @@ -1044,7 +1046,7 @@ func TestBaseTxSemanticVerifyInvalidUTXO(t *testing.T) { tx := &Tx{UnsignedTx: &BaseTx{ NetID: networkID, BCID: chainID, - Ins: []*ava.TransferableInput{&ava.TransferableInput{ + Ins: []*ava.TransferableInput{{ UTXOID: ava.UTXOID{ TxID: genesisTx.ID(), OutputIndex: math.MaxUint32, @@ -1107,7 +1109,7 @@ func TestBaseTxSemanticVerifyPendingInvalidUTXO(t *testing.T) { pendingTx := &Tx{UnsignedTx: &BaseTx{ NetID: networkID, BCID: chainID, - Ins: []*ava.TransferableInput{&ava.TransferableInput{ + Ins: []*ava.TransferableInput{{ UTXOID: ava.UTXOID{ TxID: genesisTx.ID(), OutputIndex: 1, @@ -1122,7 +1124,7 @@ func TestBaseTxSemanticVerifyPendingInvalidUTXO(t *testing.T) { }, }, }}, - Outs: []*ava.TransferableOutput{&ava.TransferableOutput{ + Outs: []*ava.TransferableOutput{{ Asset: ava.Asset{ID: genesisTx.ID()}, Out: &secp256k1fx.TransferOutput{ Amt: 50000, @@ -1179,7 +1181,7 @@ func TestBaseTxSemanticVerifyPendingInvalidUTXO(t *testing.T) { tx := &Tx{UnsignedTx: &BaseTx{ NetID: networkID, BCID: chainID, - Ins: []*ava.TransferableInput{&ava.TransferableInput{ + Ins: []*ava.TransferableInput{{ UTXOID: ava.UTXOID{ TxID: txID, OutputIndex: 2, @@ -1241,7 +1243,7 @@ func TestBaseTxSemanticVerifyPendingWrongAssetID(t *testing.T) { pendingTx := &Tx{UnsignedTx: &BaseTx{ NetID: networkID, BCID: chainID, - Ins: []*ava.TransferableInput{&ava.TransferableInput{ + Ins: []*ava.TransferableInput{{ UTXOID: ava.UTXOID{ TxID: genesisTx.ID(), OutputIndex: 1, @@ -1256,7 +1258,7 @@ func TestBaseTxSemanticVerifyPendingWrongAssetID(t *testing.T) { }, }, }}, - Outs: []*ava.TransferableOutput{&ava.TransferableOutput{ + Outs: []*ava.TransferableOutput{{ Asset: ava.Asset{ID: genesisTx.ID()}, Out: &secp256k1fx.TransferOutput{ Amt: 50000, @@ -1313,7 +1315,7 @@ func TestBaseTxSemanticVerifyPendingWrongAssetID(t *testing.T) { tx := &Tx{UnsignedTx: &BaseTx{ NetID: networkID, BCID: chainID, - Ins: []*ava.TransferableInput{&ava.TransferableInput{ + Ins: []*ava.TransferableInput{{ UTXOID: ava.UTXOID{ TxID: txID, OutputIndex: 0, @@ -1381,11 +1383,11 @@ func TestBaseTxSemanticVerifyPendingUnauthorizedFx(t *testing.T) { genesisBytes, issuer, []*common.Fx{ - &common.Fx{ + { ID: ids.NewID([32]byte{1}), Fx: &secp256k1fx.Fx{}, }, - &common.Fx{ + { ID: ids.Empty, Fx: &testFx{}, }, @@ -1419,7 +1421,7 @@ func TestBaseTxSemanticVerifyPendingUnauthorizedFx(t *testing.T) { pendingTx := &Tx{UnsignedTx: &BaseTx{ NetID: networkID, BCID: chainID, - Ins: []*ava.TransferableInput{&ava.TransferableInput{ + Ins: []*ava.TransferableInput{{ UTXOID: ava.UTXOID{ TxID: genesisTx.ID(), OutputIndex: 1, @@ -1434,7 +1436,7 @@ func TestBaseTxSemanticVerifyPendingUnauthorizedFx(t *testing.T) { }, }, }}, - Outs: []*ava.TransferableOutput{&ava.TransferableOutput{ + Outs: []*ava.TransferableOutput{{ Asset: ava.Asset{ID: genesisTx.ID()}, Out: &secp256k1fx.TransferOutput{ Amt: 50000, @@ -1491,7 +1493,7 @@ func TestBaseTxSemanticVerifyPendingUnauthorizedFx(t *testing.T) { tx := &Tx{UnsignedTx: &BaseTx{ NetID: networkID, BCID: chainID, - Ins: []*ava.TransferableInput{&ava.TransferableInput{ + Ins: []*ava.TransferableInput{{ UTXOID: ava.UTXOID{ TxID: txID, OutputIndex: 0, @@ -1543,11 +1545,11 @@ func TestBaseTxSemanticVerifyPendingInvalidSignature(t *testing.T) { genesisBytes, issuer, []*common.Fx{ - &common.Fx{ + { ID: ids.NewID([32]byte{1}), Fx: &secp256k1fx.Fx{}, }, - &common.Fx{ + { ID: ids.Empty, Fx: &testFx{}, }, @@ -1581,7 +1583,7 @@ func TestBaseTxSemanticVerifyPendingInvalidSignature(t *testing.T) { pendingTx := &Tx{UnsignedTx: &BaseTx{ NetID: networkID, BCID: chainID, - Ins: []*ava.TransferableInput{&ava.TransferableInput{ + Ins: []*ava.TransferableInput{{ UTXOID: ava.UTXOID{ TxID: genesisTx.ID(), OutputIndex: 1, @@ -1596,7 +1598,7 @@ func TestBaseTxSemanticVerifyPendingInvalidSignature(t *testing.T) { }, }, }}, - Outs: []*ava.TransferableOutput{&ava.TransferableOutput{ + Outs: []*ava.TransferableOutput{{ Asset: ava.Asset{ID: genesisTx.ID()}, Out: &secp256k1fx.TransferOutput{ Amt: 50000, @@ -1653,7 +1655,7 @@ func TestBaseTxSemanticVerifyPendingInvalidSignature(t *testing.T) { tx := &Tx{UnsignedTx: &BaseTx{ NetID: networkID, BCID: chainID, - Ins: []*ava.TransferableInput{&ava.TransferableInput{ + Ins: []*ava.TransferableInput{{ UTXOID: ava.UTXOID{ TxID: txID, OutputIndex: 0, @@ -1671,9 +1673,7 @@ func TestBaseTxSemanticVerifyPendingInvalidSignature(t *testing.T) { }} tx.Creds = append(tx.Creds, &secp256k1fx.Credential{ - Sigs: [][crypto.SECP256K1RSigLen]byte{ - [crypto.SECP256K1RSigLen]byte{}, - }, + Sigs: [][crypto.SECP256K1RSigLen]byte{{}}, }) b, err = vm.codec.Marshal(tx) diff --git a/vms/avm/create_asset_tx.go b/vms/avm/create_asset_tx.go index 9f95a15..77aae2f 100644 --- a/vms/avm/create_asset_tx.go +++ b/vms/avm/create_asset_tx.go @@ -11,7 +11,7 @@ import ( "github.com/ava-labs/gecko/snow" "github.com/ava-labs/gecko/vms/components/ava" - "github.com/ava-labs/gecko/vms/components/codec" + "github.com/ava-labs/gecko/utils/codec" ) const ( diff --git a/vms/avm/create_asset_tx_test.go b/vms/avm/create_asset_tx_test.go index a26a815..6f1f686 100644 --- a/vms/avm/create_asset_tx_test.go +++ b/vms/avm/create_asset_tx_test.go @@ -8,8 +8,8 @@ import ( "testing" "github.com/ava-labs/gecko/ids" + "github.com/ava-labs/gecko/utils/codec" "github.com/ava-labs/gecko/vms/components/ava" - "github.com/ava-labs/gecko/vms/components/codec" "github.com/ava-labs/gecko/vms/components/verify" "github.com/ava-labs/gecko/vms/secp256k1fx" ) @@ -93,7 +93,7 @@ func TestCreateAssetTxSerialization(t *testing.T) { 0xbb, 0xbb, 0xbb, 0xbb, 0xaa, 0xaa, 0xaa, 0xaa, 0x99, 0x99, 0x99, 0x99, 0x88, 0x88, 0x88, 0x88, }), - Outs: []*ava.TransferableOutput{&ava.TransferableOutput{ + Outs: []*ava.TransferableOutput{{ Asset: ava.Asset{ ID: ids.NewID([32]byte{ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, @@ -122,7 +122,7 @@ func TestCreateAssetTxSerialization(t *testing.T) { }, }, }}, - Ins: []*ava.TransferableInput{&ava.TransferableInput{ + Ins: []*ava.TransferableInput{{ UTXOID: ava.UTXOID{ TxID: ids.NewID([32]byte{ 0xf1, 0xe1, 0xd1, 0xc1, 0xb1, 0xa1, 0x91, 0x81, @@ -152,7 +152,7 @@ func TestCreateAssetTxSerialization(t *testing.T) { Symbol: "VIX", Denomination: 2, States: []*InitialState{ - &InitialState{ + { FxID: 0, Outs: []verify.Verifiable{ &secp256k1fx.TransferOutput{ diff --git a/vms/avm/export_tx.go b/vms/avm/export_tx.go index d5222f4..d788360 100644 --- a/vms/avm/export_tx.go +++ b/vms/avm/export_tx.go @@ -9,7 +9,7 @@ import ( "github.com/ava-labs/gecko/database/versiondb" "github.com/ava-labs/gecko/snow" "github.com/ava-labs/gecko/vms/components/ava" - "github.com/ava-labs/gecko/vms/components/codec" + "github.com/ava-labs/gecko/utils/codec" "github.com/ava-labs/gecko/vms/components/verify" ) diff --git a/vms/avm/export_tx_test.go b/vms/avm/export_tx_test.go index 4e9d064..96c7733 100644 --- a/vms/avm/export_tx_test.go +++ b/vms/avm/export_tx_test.go @@ -9,14 +9,15 @@ import ( "github.com/ava-labs/gecko/chains/atomic" "github.com/ava-labs/gecko/database/memdb" + "github.com/ava-labs/gecko/database/prefixdb" "github.com/ava-labs/gecko/ids" "github.com/ava-labs/gecko/snow" "github.com/ava-labs/gecko/snow/engine/common" + "github.com/ava-labs/gecko/utils/codec" "github.com/ava-labs/gecko/utils/crypto" "github.com/ava-labs/gecko/utils/hashing" "github.com/ava-labs/gecko/utils/logging" "github.com/ava-labs/gecko/vms/components/ava" - "github.com/ava-labs/gecko/vms/components/codec" "github.com/ava-labs/gecko/vms/secp256k1fx" ) @@ -68,7 +69,7 @@ func TestExportTxSerialization(t *testing.T) { 0xbb, 0xbb, 0xbb, 0xbb, 0xaa, 0xaa, 0xaa, 0xaa, 0x99, 0x99, 0x99, 0x99, 0x88, 0x88, 0x88, 0x88, }), - Ins: []*ava.TransferableInput{&ava.TransferableInput{ + Ins: []*ava.TransferableInput{{ UTXOID: ava.UTXOID{TxID: ids.NewID([32]byte{ 0x0f, 0x2f, 0x4f, 0x6f, 0x8e, 0xae, 0xce, 0xee, 0x0d, 0x2d, 0x4d, 0x6d, 0x8c, 0xac, 0xcc, 0xec, @@ -117,9 +118,10 @@ func TestIssueExportTx(t *testing.T) { genesisBytes := BuildGenesisTest(t) issuer := make(chan common.Message, 1) + baseDB := memdb.New() sm := &atomic.SharedMemory{} - sm.Initialize(logging.NoLog{}, memdb.New()) + sm.Initialize(logging.NoLog{}, prefixdb.New([]byte{0}, baseDB)) ctx := snow.DefaultContextTest() ctx.NetworkID = networkID @@ -138,10 +140,10 @@ func TestIssueExportTx(t *testing.T) { } err := vm.Initialize( ctx, - memdb.New(), + prefixdb.New([]byte{1}, baseDB), genesisBytes, issuer, - []*common.Fx{&common.Fx{ + []*common.Fx{{ ID: ids.Empty, Fx: &secp256k1fx.Fx{}, }}, @@ -167,7 +169,7 @@ func TestIssueExportTx(t *testing.T) { BaseTx: BaseTx{ NetID: networkID, BCID: chainID, - Ins: []*ava.TransferableInput{&ava.TransferableInput{ + Ins: []*ava.TransferableInput{{ UTXOID: ava.UTXOID{ TxID: avaID, OutputIndex: 1, @@ -179,7 +181,7 @@ func TestIssueExportTx(t *testing.T) { }, }}, }, - Outs: []*ava.TransferableOutput{&ava.TransferableOutput{ + Outs: []*ava.TransferableOutput{{ Asset: ava.Asset{ID: avaID}, Out: &secp256k1fx.TransferOutput{ Amt: 50000, @@ -273,9 +275,10 @@ func TestClearForceAcceptedExportTx(t *testing.T) { genesisBytes := BuildGenesisTest(t) issuer := make(chan common.Message, 1) + baseDB := memdb.New() sm := &atomic.SharedMemory{} - sm.Initialize(logging.NoLog{}, memdb.New()) + sm.Initialize(logging.NoLog{}, prefixdb.New([]byte{0}, baseDB)) ctx := snow.DefaultContextTest() ctx.NetworkID = networkID @@ -294,10 +297,10 @@ func TestClearForceAcceptedExportTx(t *testing.T) { } err := vm.Initialize( ctx, - memdb.New(), + prefixdb.New([]byte{1}, baseDB), genesisBytes, issuer, - []*common.Fx{&common.Fx{ + []*common.Fx{{ ID: ids.Empty, Fx: &secp256k1fx.Fx{}, }}, @@ -323,7 +326,7 @@ func TestClearForceAcceptedExportTx(t *testing.T) { BaseTx: BaseTx{ NetID: networkID, BCID: chainID, - Ins: []*ava.TransferableInput{&ava.TransferableInput{ + Ins: []*ava.TransferableInput{{ UTXOID: ava.UTXOID{ TxID: avaID, OutputIndex: 1, @@ -335,7 +338,7 @@ func TestClearForceAcceptedExportTx(t *testing.T) { }, }}, }, - Outs: []*ava.TransferableOutput{&ava.TransferableOutput{ + Outs: []*ava.TransferableOutput{{ Asset: ava.Asset{ID: avaID}, Out: &secp256k1fx.TransferOutput{ Amt: 50000, diff --git a/vms/avm/import_tx.go b/vms/avm/import_tx.go index 09dec6e..caf0f93 100644 --- a/vms/avm/import_tx.go +++ b/vms/avm/import_tx.go @@ -11,8 +11,8 @@ import ( "github.com/ava-labs/gecko/database/versiondb" "github.com/ava-labs/gecko/ids" "github.com/ava-labs/gecko/snow" + "github.com/ava-labs/gecko/utils/codec" "github.com/ava-labs/gecko/vms/components/ava" - "github.com/ava-labs/gecko/vms/components/codec" "github.com/ava-labs/gecko/vms/components/verify" ) @@ -33,6 +33,15 @@ func (t *ImportTx) InputUTXOs() []*ava.UTXOID { return utxos } +// ConsumedAssetIDs returns the IDs of the assets this transaction consumes +func (t *ImportTx) ConsumedAssetIDs() ids.Set { + assets := t.BaseTx.AssetIDs() + for _, in := range t.Ins { + assets.Add(in.AssetID()) + } + return assets +} + // AssetIDs returns the IDs of the assets this transaction depends on func (t *ImportTx) AssetIDs() ids.Set { assets := t.BaseTx.AssetIDs() diff --git a/vms/avm/import_tx_test.go b/vms/avm/import_tx_test.go index 696e841..750d402 100644 --- a/vms/avm/import_tx_test.go +++ b/vms/avm/import_tx_test.go @@ -9,6 +9,7 @@ import ( "github.com/ava-labs/gecko/chains/atomic" "github.com/ava-labs/gecko/database/memdb" + "github.com/ava-labs/gecko/database/prefixdb" "github.com/ava-labs/gecko/ids" "github.com/ava-labs/gecko/snow" "github.com/ava-labs/gecko/snow/engine/common" @@ -68,7 +69,7 @@ func TestImportTxSerialization(t *testing.T) { 0x99, 0x99, 0x99, 0x99, 0x88, 0x88, 0x88, 0x88, }), }, - Ins: []*ava.TransferableInput{&ava.TransferableInput{ + Ins: []*ava.TransferableInput{{ UTXOID: ava.UTXOID{TxID: ids.NewID([32]byte{ 0x0f, 0x2f, 0x4f, 0x6f, 0x8e, 0xae, 0xce, 0xee, 0x0d, 0x2d, 0x4d, 0x6d, 0x8c, 0xac, 0xcc, 0xec, @@ -106,9 +107,10 @@ func TestIssueImportTx(t *testing.T) { genesisBytes := BuildGenesisTest(t) issuer := make(chan common.Message, 1) + baseDB := memdb.New() sm := &atomic.SharedMemory{} - sm.Initialize(logging.NoLog{}, memdb.New()) + sm.Initialize(logging.NoLog{}, prefixdb.New([]byte{0}, baseDB)) ctx := snow.DefaultContextTest() ctx.NetworkID = networkID @@ -127,10 +129,10 @@ func TestIssueImportTx(t *testing.T) { } err := vm.Initialize( ctx, - memdb.New(), + prefixdb.New([]byte{1}, baseDB), genesisBytes, issuer, - []*common.Fx{&common.Fx{ + []*common.Fx{{ ID: ids.Empty, Fx: &secp256k1fx.Fx{}, }}, @@ -166,7 +168,7 @@ func TestIssueImportTx(t *testing.T) { NetID: networkID, BCID: chainID, }, - Ins: []*ava.TransferableInput{&ava.TransferableInput{ + Ins: []*ava.TransferableInput{{ UTXOID: utxoID, Asset: ava.Asset{ID: avaID}, In: &secp256k1fx.TransferInput{ @@ -265,9 +267,10 @@ func TestForceAcceptImportTx(t *testing.T) { genesisBytes := BuildGenesisTest(t) issuer := make(chan common.Message, 1) + baseDB := memdb.New() sm := &atomic.SharedMemory{} - sm.Initialize(logging.NoLog{}, memdb.New()) + sm.Initialize(logging.NoLog{}, prefixdb.New([]byte{0}, baseDB)) ctx := snow.DefaultContextTest() ctx.NetworkID = networkID @@ -285,10 +288,10 @@ func TestForceAcceptImportTx(t *testing.T) { err := vm.Initialize( ctx, - memdb.New(), + prefixdb.New([]byte{1}, baseDB), genesisBytes, issuer, - []*common.Fx{&common.Fx{ + []*common.Fx{{ ID: ids.Empty, Fx: &secp256k1fx.Fx{}, }}, @@ -326,7 +329,7 @@ func TestForceAcceptImportTx(t *testing.T) { NetID: networkID, BCID: chainID, }, - Ins: []*ava.TransferableInput{&ava.TransferableInput{ + Ins: []*ava.TransferableInput{{ UTXOID: utxoID, Asset: ava.Asset{ID: genesisTx.ID()}, In: &secp256k1fx.TransferInput{ diff --git a/vms/avm/initial_state.go b/vms/avm/initial_state.go index c3d4b16..73ad6e4 100644 --- a/vms/avm/initial_state.go +++ b/vms/avm/initial_state.go @@ -8,7 +8,7 @@ import ( "errors" "sort" - "github.com/ava-labs/gecko/vms/components/codec" + "github.com/ava-labs/gecko/utils/codec" "github.com/ava-labs/gecko/vms/components/verify" ) diff --git a/vms/avm/initial_state_test.go b/vms/avm/initial_state_test.go index 67c4b15..b61876c 100644 --- a/vms/avm/initial_state_test.go +++ b/vms/avm/initial_state_test.go @@ -11,7 +11,7 @@ import ( "github.com/ava-labs/gecko/ids" "github.com/ava-labs/gecko/utils/formatting" "github.com/ava-labs/gecko/vms/components/ava" - "github.com/ava-labs/gecko/vms/components/codec" + "github.com/ava-labs/gecko/utils/codec" "github.com/ava-labs/gecko/vms/components/verify" "github.com/ava-labs/gecko/vms/secp256k1fx" ) diff --git a/vms/avm/operation.go b/vms/avm/operation.go index 3b5fc9a..ef9317b 100644 --- a/vms/avm/operation.go +++ b/vms/avm/operation.go @@ -10,7 +10,7 @@ import ( "github.com/ava-labs/gecko/utils" "github.com/ava-labs/gecko/vms/components/ava" - "github.com/ava-labs/gecko/vms/components/codec" + "github.com/ava-labs/gecko/utils/codec" "github.com/ava-labs/gecko/vms/components/verify" ) diff --git a/vms/avm/operation_test.go b/vms/avm/operation_test.go index 8948388..b3aed54 100644 --- a/vms/avm/operation_test.go +++ b/vms/avm/operation_test.go @@ -7,8 +7,8 @@ import ( "testing" "github.com/ava-labs/gecko/ids" + "github.com/ava-labs/gecko/utils/codec" "github.com/ava-labs/gecko/vms/components/ava" - "github.com/ava-labs/gecko/vms/components/codec" "github.com/ava-labs/gecko/vms/components/verify" ) @@ -43,11 +43,11 @@ func TestOperationVerifyUTXOIDsNotSorted(t *testing.T) { op := &Operation{ Asset: ava.Asset{ID: ids.Empty}, UTXOIDs: []*ava.UTXOID{ - &ava.UTXOID{ + { TxID: ids.Empty, OutputIndex: 1, }, - &ava.UTXOID{ + { TxID: ids.Empty, OutputIndex: 0, }, @@ -64,7 +64,7 @@ func TestOperationVerify(t *testing.T) { op := &Operation{ Asset: ava.Asset{ID: ids.Empty}, UTXOIDs: []*ava.UTXOID{ - &ava.UTXOID{ + { TxID: ids.Empty, OutputIndex: 1, }, @@ -81,20 +81,20 @@ func TestOperationSorting(t *testing.T) { c.RegisterType(&testOperable{}) ops := []*Operation{ - &Operation{ + { Asset: ava.Asset{ID: ids.Empty}, UTXOIDs: []*ava.UTXOID{ - &ava.UTXOID{ + { TxID: ids.Empty, OutputIndex: 1, }, }, Op: &testOperable{}, }, - &Operation{ + { Asset: ava.Asset{ID: ids.Empty}, UTXOIDs: []*ava.UTXOID{ - &ava.UTXOID{ + { TxID: ids.Empty, OutputIndex: 0, }, @@ -112,7 +112,7 @@ func TestOperationSorting(t *testing.T) { ops = append(ops, &Operation{ Asset: ava.Asset{ID: ids.Empty}, UTXOIDs: []*ava.UTXOID{ - &ava.UTXOID{ + { TxID: ids.Empty, OutputIndex: 1, }, diff --git a/vms/avm/operation_tx.go b/vms/avm/operation_tx.go index 9384f8d..c45f0be 100644 --- a/vms/avm/operation_tx.go +++ b/vms/avm/operation_tx.go @@ -8,8 +8,8 @@ import ( "github.com/ava-labs/gecko/ids" "github.com/ava-labs/gecko/snow" + "github.com/ava-labs/gecko/utils/codec" "github.com/ava-labs/gecko/vms/components/ava" - "github.com/ava-labs/gecko/vms/components/codec" "github.com/ava-labs/gecko/vms/components/verify" ) @@ -39,6 +39,17 @@ func (t *OperationTx) InputUTXOs() []*ava.UTXOID { return utxos } +// ConsumedAssetIDs returns the IDs of the assets this transaction consumes +func (t *OperationTx) ConsumedAssetIDs() ids.Set { + assets := t.BaseTx.AssetIDs() + for _, op := range t.Ops { + if len(op.UTXOIDs) > 0 { + assets.Add(op.AssetID()) + } + } + return assets +} + // AssetIDs returns the IDs of the assets this transaction depends on func (t *OperationTx) AssetIDs() ids.Set { assets := t.BaseTx.AssetIDs() diff --git a/vms/avm/prefixed_state_test.go b/vms/avm/prefixed_state_test.go index 74c4b53..e325142 100644 --- a/vms/avm/prefixed_state_test.go +++ b/vms/avm/prefixed_state_test.go @@ -38,7 +38,7 @@ func TestPrefixedSetsAndGets(t *testing.T) { tx := &Tx{UnsignedTx: &BaseTx{ NetID: networkID, BCID: chainID, - Ins: []*ava.TransferableInput{&ava.TransferableInput{ + Ins: []*ava.TransferableInput{{ UTXOID: ava.UTXOID{ TxID: ids.Empty, OutputIndex: 0, @@ -160,9 +160,7 @@ func TestPrefixedFundingAddresses(t *testing.T) { }, Asset: ava.Asset{ID: ids.Empty}, Out: &ava.TestAddressable{ - Addrs: [][]byte{ - []byte{0}, - }, + Addrs: [][]byte{{0}}, }, } diff --git a/vms/avm/service.go b/vms/avm/service.go index f71d607..3d33118 100644 --- a/vms/avm/service.go +++ b/vms/avm/service.go @@ -56,7 +56,7 @@ type IssueTxReply struct { // IssueTx attempts to issue a transaction into consensus func (service *Service) IssueTx(r *http.Request, args *IssueTxArgs, reply *IssueTxReply) error { - service.vm.ctx.Log.Verbo("IssueTx called with %s", args.Tx) + service.vm.ctx.Log.Info("AVM: IssueTx called with %s", args.Tx) txID, err := service.vm.IssueTx(args.Tx.Bytes, nil) if err != nil { @@ -79,7 +79,7 @@ type GetTxStatusReply struct { // GetTxStatus returns the status of the specified transaction func (service *Service) GetTxStatus(r *http.Request, args *GetTxStatusArgs, reply *GetTxStatusReply) error { - service.vm.ctx.Log.Verbo("GetTxStatus called with %s", args.TxID) + service.vm.ctx.Log.Info("AVM: GetTxStatus called with %s", args.TxID) if args.TxID.IsZero() { return errNilTxID @@ -106,7 +106,7 @@ type GetTxReply struct { // GetTx returns the specified transaction func (service *Service) GetTx(r *http.Request, args *GetTxArgs, reply *GetTxReply) error { - service.vm.ctx.Log.Verbo("GetTx called with %s", args.TxID) + service.vm.ctx.Log.Info("AVM: GetTx called with %s", args.TxID) if args.TxID.IsZero() { return errNilTxID @@ -136,7 +136,7 @@ type GetUTXOsReply struct { // GetUTXOs creates an empty account with the name passed in func (service *Service) GetUTXOs(r *http.Request, args *GetUTXOsArgs, reply *GetUTXOsReply) error { - service.vm.ctx.Log.Verbo("GetUTXOs called with %s", args.Addresses) + service.vm.ctx.Log.Info("AVM: GetUTXOs called with %s", args.Addresses) addrSet := ids.Set{} for _, addr := range args.Addresses { @@ -178,7 +178,7 @@ type GetAssetDescriptionReply struct { // GetAssetDescription creates an empty account with the name passed in func (service *Service) GetAssetDescription(_ *http.Request, args *GetAssetDescriptionArgs, reply *GetAssetDescriptionReply) error { - service.vm.ctx.Log.Verbo("GetAssetDescription called with %s", args.AssetID) + service.vm.ctx.Log.Info("AVM: GetAssetDescription called with %s", args.AssetID) assetID, err := service.vm.Lookup(args.AssetID) if err != nil { @@ -222,7 +222,7 @@ type GetBalanceReply struct { // GetBalance returns the amount of an asset that an address at least partially owns func (service *Service) GetBalance(r *http.Request, args *GetBalanceArgs, reply *GetBalanceReply) error { - service.vm.ctx.Log.Verbo("GetBalance called with address: %s assetID: %s", args.Address, args.AssetID) + service.vm.ctx.Log.Info("AVM: GetBalance called with address: %s assetID: %s", args.Address, args.AssetID) address, err := service.vm.Parse(args.Address) if err != nil { @@ -287,7 +287,7 @@ type GetAllBalancesReply struct { // Note that balances include assets that the address only _partially_ owns // (ie is one of several addresses specified in a multi-sig) func (service *Service) GetAllBalances(r *http.Request, args *GetAllBalancesArgs, reply *GetAllBalancesReply) error { - service.vm.ctx.Log.Verbo("GetAllBalances called with address: %s", args.Address) + service.vm.ctx.Log.Info("AVM: GetAllBalances called with address: %s", args.Address) address, err := service.vm.Parse(args.Address) if err != nil { @@ -360,7 +360,7 @@ type CreateFixedCapAssetReply struct { // CreateFixedCapAsset returns ID of the newly created asset func (service *Service) CreateFixedCapAsset(r *http.Request, args *CreateFixedCapAssetArgs, reply *CreateFixedCapAssetReply) error { - service.vm.ctx.Log.Verbo("CreateFixedCapAsset called with name: %s symbol: %s number of holders: %d", + service.vm.ctx.Log.Info("AVM: CreateFixedCapAsset called with name: %s symbol: %s number of holders: %d", args.Name, args.Symbol, len(args.InitialHolders), @@ -445,7 +445,7 @@ type CreateVariableCapAssetReply struct { // CreateVariableCapAsset returns ID of the newly created asset func (service *Service) CreateVariableCapAsset(r *http.Request, args *CreateVariableCapAssetArgs, reply *CreateVariableCapAssetReply) error { - service.vm.ctx.Log.Verbo("CreateFixedCapAsset called with name: %s symbol: %s number of minters: %d", + service.vm.ctx.Log.Info("AVM: CreateFixedCapAsset called with name: %s symbol: %s number of minters: %d", args.Name, args.Symbol, len(args.MinterSets), @@ -523,7 +523,7 @@ type CreateAddressReply struct { // CreateAddress creates an address for the user [args.Username] func (service *Service) CreateAddress(r *http.Request, args *CreateAddressArgs, reply *CreateAddressReply) error { - service.vm.ctx.Log.Verbo("CreateAddress called for user '%s'", args.Username) + service.vm.ctx.Log.Info("AVM: CreateAddress called for user '%s'", args.Username) db, err := service.vm.ctx.Keystore.GetDatabase(args.Username, args.Password) if err != nil { @@ -603,7 +603,7 @@ type ExportKeyReply struct { // ExportKey returns a private key from the provided user func (service *Service) ExportKey(r *http.Request, args *ExportKeyArgs, reply *ExportKeyReply) error { - service.vm.ctx.Log.Verbo("ExportKey called for user '%s'", args.Username) + service.vm.ctx.Log.Info("AVM: ExportKey called for user '%s'", args.Username) address, err := service.vm.Parse(args.Address) if err != nil { @@ -645,7 +645,7 @@ type ImportKeyReply struct { // ImportKey adds a private key to the provided user func (service *Service) ImportKey(r *http.Request, args *ImportKeyArgs, reply *ImportKeyReply) error { - service.vm.ctx.Log.Verbo("ImportKey called for user '%s'", args.Username) + service.vm.ctx.Log.Info("AVM: ImportKey called for user '%s'", args.Username) db, err := service.vm.ctx.Keystore.GetDatabase(args.Username, args.Password) if err != nil { @@ -666,13 +666,20 @@ func (service *Service) ImportKey(r *http.Request, args *ImportKeyArgs, reply *I } addresses, _ := user.Addresses(db) - addresses = append(addresses, sk.PublicKey().Address()) + newAddress := sk.PublicKey().Address() + reply.Address = service.vm.Format(newAddress.Bytes()) + for _, address := range addresses { + if newAddress.Equals(address) { + return nil + } + } + + addresses = append(addresses, newAddress) if err := user.SetAddresses(db, addresses); err != nil { return fmt.Errorf("problem saving addresses: %w", err) } - reply.Address = service.vm.Format(sk.PublicKey().Address().Bytes()) return nil } @@ -692,7 +699,7 @@ type SendReply struct { // Send returns the ID of the newly created transaction func (service *Service) Send(r *http.Request, args *SendArgs, reply *SendReply) error { - service.vm.ctx.Log.Verbo("Send called with username: %s", args.Username) + service.vm.ctx.Log.Info("AVM: Send called with username: %s", args.Username) if args.Amount == 0 { return errInvalidAmount @@ -785,7 +792,7 @@ func (service *Service) Send(r *http.Request, args *SendArgs, reply *SendReply) ava.SortTransferableInputsWithSigners(ins, keys) - outs := []*ava.TransferableOutput{&ava.TransferableOutput{ + outs := []*ava.TransferableOutput{{ Asset: ava.Asset{ID: assetID}, Out: &secp256k1fx.TransferOutput{ Amt: uint64(args.Amount), @@ -873,7 +880,7 @@ type CreateMintTxReply struct { // CreateMintTx returns the newly created unsigned transaction func (service *Service) CreateMintTx(r *http.Request, args *CreateMintTxArgs, reply *CreateMintTxReply) error { - service.vm.ctx.Log.Verbo("CreateMintTx called") + service.vm.ctx.Log.Info("AVM: CreateMintTx called") if args.Amount == 0 { return errInvalidMintAmount @@ -939,7 +946,7 @@ func (service *Service) CreateMintTx(r *http.Request, args *CreateMintTxArgs, re BCID: service.vm.ctx.ChainID, }, Ops: []*Operation{ - &Operation{ + { Asset: ava.Asset{ID: assetID}, UTXOIDs: []*ava.UTXOID{ &utxo.UTXOID, @@ -990,7 +997,7 @@ type SignMintTxReply struct { // SignMintTx returns the newly signed transaction func (service *Service) SignMintTx(r *http.Request, args *SignMintTxArgs, reply *SignMintTxReply) error { - service.vm.ctx.Log.Verbo("SignMintTx called") + service.vm.ctx.Log.Info("AVM: SignMintTx called") minter, err := service.vm.Parse(args.Minter) if err != nil { @@ -1116,7 +1123,7 @@ type ImportAVAReply struct { // The AVA must have already been exported from the P-Chain. // Returns the ID of the newly created atomic transaction func (service *Service) ImportAVA(_ *http.Request, args *ImportAVAArgs, reply *ImportAVAReply) error { - service.vm.ctx.Log.Verbo("ImportAVA called with username: %s", args.Username) + service.vm.ctx.Log.Info("AVM: ImportAVA called with username: %s", args.Username) toBytes, err := service.vm.Parse(args.To) if err != nil { @@ -1190,7 +1197,7 @@ func (service *Service) ImportAVA(_ *http.Request, args *ImportAVAArgs, reply *I ava.SortTransferableInputsWithSigners(ins, keys) - outs := []*ava.TransferableOutput{&ava.TransferableOutput{ + outs := []*ava.TransferableOutput{{ Asset: ava.Asset{ID: service.vm.ava}, Out: &secp256k1fx.TransferOutput{ Amt: amount, @@ -1268,7 +1275,7 @@ type ExportAVAReply struct { // After this tx is accepted, the AVA must be imported to the P-chain with an importTx. // Returns the ID of the newly created atomic transaction func (service *Service) ExportAVA(_ *http.Request, args *ExportAVAArgs, reply *ExportAVAReply) error { - service.vm.ctx.Log.Verbo("ExportAVA called with username: %s", args.Username) + service.vm.ctx.Log.Info("AVM: ExportAVA called with username: %s", args.Username) if args.Amount == 0 { return errInvalidAmount @@ -1345,7 +1352,7 @@ func (service *Service) ExportAVA(_ *http.Request, args *ExportAVAArgs, reply *E ava.SortTransferableInputsWithSigners(ins, keys) - exportOuts := []*ava.TransferableOutput{&ava.TransferableOutput{ + exportOuts := []*ava.TransferableOutput{{ Asset: ava.Asset{ID: service.vm.ava}, Out: &secp256k1fx.TransferOutput{ Amt: uint64(args.Amount), diff --git a/vms/avm/service_test.go b/vms/avm/service_test.go index fdd8053..b8a7d56 100644 --- a/vms/avm/service_test.go +++ b/vms/avm/service_test.go @@ -9,8 +9,10 @@ import ( "github.com/stretchr/testify/assert" + "github.com/ava-labs/gecko/api/keystore" "github.com/ava-labs/gecko/ids" "github.com/ava-labs/gecko/snow/choices" + "github.com/ava-labs/gecko/utils/crypto" "github.com/ava-labs/gecko/utils/formatting" ) @@ -298,7 +300,7 @@ func TestCreateFixedCapAsset(t *testing.T) { Name: "test asset", Symbol: "test", Denomination: 1, - InitialHolders: []*Holder{&Holder{ + InitialHolders: []*Holder{{ Amount: 123456789, Address: vm.Format(keys[0].PublicKey().Address().Bytes()), }}, @@ -324,7 +326,7 @@ func TestCreateVariableCapAsset(t *testing.T) { Name: "test asset", Symbol: "test", MinterSets: []Owners{ - Owners{ + { Threshold: 1, Minters: []string{ vm.Format(keys[0].PublicKey().Address().Bytes()), @@ -340,3 +342,113 @@ func TestCreateVariableCapAsset(t *testing.T) { t.Fatalf("Wrong assetID returned from CreateFixedCapAsset %s", reply.AssetID) } } + +func TestImportAvmKey(t *testing.T) { + _, vm, s := setup(t) + defer func() { + vm.Shutdown() + ctx.Lock.Unlock() + }() + + userKeystore := keystore.CreateTestKeystore(t) + + username := "bobby" + password := "StrnasfqewiurPasswdn56d" + if err := userKeystore.AddUser(username, password); err != nil { + t.Fatal(err) + } + + vm.ctx.Keystore = userKeystore.NewBlockchainKeyStore(vm.ctx.ChainID) + _, err := vm.ctx.Keystore.GetDatabase(username, password) + if err != nil { + t.Fatal(err) + } + + factory := crypto.FactorySECP256K1R{} + skIntf, err := factory.NewPrivateKey() + if err != nil { + t.Fatalf("problem generating private key: %s", err) + } + sk := skIntf.(*crypto.PrivateKeySECP256K1R) + + args := ImportKeyArgs{ + Username: username, + Password: password, + PrivateKey: formatting.CB58{Bytes: sk.Bytes()}, + } + reply := ImportKeyReply{} + if err = s.ImportKey(nil, &args, &reply); err != nil { + t.Fatal(err) + } +} + +func TestImportAvmKeyNoDuplicates(t *testing.T) { + _, vm, s := setup(t) + defer func() { + vm.Shutdown() + ctx.Lock.Unlock() + }() + + userKeystore := keystore.CreateTestKeystore(t) + + username := "bobby" + password := "StrnasfqewiurPasswdn56d" + if err := userKeystore.AddUser(username, password); err != nil { + t.Fatal(err) + } + + vm.ctx.Keystore = userKeystore.NewBlockchainKeyStore(vm.ctx.ChainID) + _, err := vm.ctx.Keystore.GetDatabase(username, password) + if err != nil { + t.Fatal(err) + } + + factory := crypto.FactorySECP256K1R{} + skIntf, err := factory.NewPrivateKey() + if err != nil { + t.Fatalf("problem generating private key: %s", err) + } + sk := skIntf.(*crypto.PrivateKeySECP256K1R) + + args := ImportKeyArgs{ + Username: username, + Password: password, + PrivateKey: formatting.CB58{Bytes: sk.Bytes()}, + } + reply := ImportKeyReply{} + if err = s.ImportKey(nil, &args, &reply); err != nil { + t.Fatal(err) + } + + expectedAddress := vm.Format(sk.PublicKey().Address().Bytes()) + + if reply.Address != expectedAddress { + t.Fatalf("Reply address: %s did not match expected address: %s", reply.Address, expectedAddress) + } + + reply2 := ImportKeyReply{} + if err = s.ImportKey(nil, &args, &reply2); err != nil { + t.Fatal(err) + } + + if reply2.Address != expectedAddress { + t.Fatalf("Reply address: %s did not match expected address: %s", reply2.Address, expectedAddress) + } + + addrsArgs := ListAddressesArgs{ + Username: username, + Password: password, + } + addrsReply := ListAddressesResponse{} + if err := s.ListAddresses(nil, &addrsArgs, &addrsReply); err != nil { + t.Fatal(err) + } + + if len(addrsReply.Addresses) != 1 { + t.Fatal("Importing the same key twice created duplicate addresses") + } + + if addrsReply.Addresses[0] != expectedAddress { + t.Fatal("List addresses returned an incorrect address") + } +} diff --git a/vms/avm/state_test.go b/vms/avm/state_test.go index 1feabab..3617ddc 100644 --- a/vms/avm/state_test.go +++ b/vms/avm/state_test.go @@ -288,7 +288,7 @@ func TestStateTXs(t *testing.T) { tx := &Tx{UnsignedTx: &BaseTx{ NetID: networkID, BCID: chainID, - Ins: []*ava.TransferableInput{&ava.TransferableInput{ + Ins: []*ava.TransferableInput{{ UTXOID: ava.UTXOID{ TxID: ids.Empty, OutputIndex: 0, diff --git a/vms/avm/static_service.go b/vms/avm/static_service.go index 3fd58f3..48b58a9 100644 --- a/vms/avm/static_service.go +++ b/vms/avm/static_service.go @@ -11,7 +11,7 @@ import ( "github.com/ava-labs/gecko/ids" "github.com/ava-labs/gecko/utils/formatting" "github.com/ava-labs/gecko/utils/wrappers" - "github.com/ava-labs/gecko/vms/components/codec" + "github.com/ava-labs/gecko/utils/codec" "github.com/ava-labs/gecko/vms/secp256k1fx" cjson "github.com/ava-labs/gecko/utils/json" diff --git a/vms/avm/static_service_test.go b/vms/avm/static_service_test.go index fd9acc0..34e8803 100644 --- a/vms/avm/static_service_test.go +++ b/vms/avm/static_service_test.go @@ -11,12 +11,12 @@ func TestBuildGenesis(t *testing.T) { ss := StaticService{} args := BuildGenesisArgs{GenesisData: map[string]AssetDefinition{ - "asset1": AssetDefinition{ + "asset1": { Name: "myFixedCapAsset", Symbol: "MFCA", Denomination: 8, InitialState: map[string][]interface{}{ - "fixedCap": []interface{}{ + "fixedCap": { Holder{ Amount: 100000, Address: "A9bTQjfYGBFK3JPRJqF2eh3JYL7cHocvy", @@ -36,11 +36,11 @@ func TestBuildGenesis(t *testing.T) { }, }, }, - "asset2": AssetDefinition{ + "asset2": { Name: "myVarCapAsset", Symbol: "MVCA", InitialState: map[string][]interface{}{ - "variableCap": []interface{}{ + "variableCap": { Owners{ Threshold: 1, Minters: []string{ @@ -58,10 +58,10 @@ func TestBuildGenesis(t *testing.T) { }, }, }, - "asset3": AssetDefinition{ + "asset3": { Name: "myOtherVarCapAsset", InitialState: map[string][]interface{}{ - "variableCap": []interface{}{ + "variableCap": { Owners{ Threshold: 1, Minters: []string{ diff --git a/vms/avm/tx.go b/vms/avm/tx.go index c35fd80..6c32153 100644 --- a/vms/avm/tx.go +++ b/vms/avm/tx.go @@ -9,8 +9,8 @@ import ( "github.com/ava-labs/gecko/database" "github.com/ava-labs/gecko/ids" "github.com/ava-labs/gecko/snow" + "github.com/ava-labs/gecko/utils/codec" "github.com/ava-labs/gecko/vms/components/ava" - "github.com/ava-labs/gecko/vms/components/codec" "github.com/ava-labs/gecko/vms/components/verify" ) @@ -24,7 +24,9 @@ type UnsignedTx interface { ID() ids.ID Bytes() []byte + ConsumedAssetIDs() ids.Set AssetIDs() ids.Set + NumCredentials() int InputUTXOs() []*ava.UTXOID UTXOs() []*ava.UTXO diff --git a/vms/avm/tx_test.go b/vms/avm/tx_test.go index 2f269e9..f088d9e 100644 --- a/vms/avm/tx_test.go +++ b/vms/avm/tx_test.go @@ -7,9 +7,9 @@ import ( "testing" "github.com/ava-labs/gecko/ids" + "github.com/ava-labs/gecko/utils/codec" "github.com/ava-labs/gecko/utils/units" "github.com/ava-labs/gecko/vms/components/ava" - "github.com/ava-labs/gecko/vms/components/codec" "github.com/ava-labs/gecko/vms/components/verify" "github.com/ava-labs/gecko/vms/secp256k1fx" ) @@ -56,7 +56,7 @@ func TestTxInvalidCredential(t *testing.T) { UnsignedTx: &BaseTx{ NetID: networkID, BCID: chainID, - Ins: []*ava.TransferableInput{&ava.TransferableInput{ + Ins: []*ava.TransferableInput{{ UTXOID: ava.UTXOID{ TxID: ids.Empty, OutputIndex: 0, @@ -95,7 +95,7 @@ func TestTxInvalidUnsignedTx(t *testing.T) { NetID: networkID, BCID: chainID, Ins: []*ava.TransferableInput{ - &ava.TransferableInput{ + { UTXOID: ava.UTXOID{ TxID: ids.Empty, OutputIndex: 0, @@ -110,7 +110,7 @@ func TestTxInvalidUnsignedTx(t *testing.T) { }, }, }, - &ava.TransferableInput{ + { UTXOID: ava.UTXOID{ TxID: ids.Empty, OutputIndex: 0, @@ -153,7 +153,7 @@ func TestTxInvalidNumberOfCredentials(t *testing.T) { NetID: networkID, BCID: chainID, Ins: []*ava.TransferableInput{ - &ava.TransferableInput{ + { UTXOID: ava.UTXOID{TxID: ids.Empty, OutputIndex: 0}, Asset: ava.Asset{ID: asset}, In: &secp256k1fx.TransferInput{ @@ -165,7 +165,7 @@ func TestTxInvalidNumberOfCredentials(t *testing.T) { }, }, }, - &ava.TransferableInput{ + { UTXOID: ava.UTXOID{TxID: ids.Empty, OutputIndex: 1}, Asset: ava.Asset{ID: asset}, In: &secp256k1fx.TransferInput{ diff --git a/vms/avm/unique_tx.go b/vms/avm/unique_tx.go index 8d3feb2..576702e 100644 --- a/vms/avm/unique_tx.go +++ b/vms/avm/unique_tx.go @@ -85,7 +85,11 @@ func (tx *UniqueTx) refresh() { // Evict is called when this UniqueTx will no longer be returned from a cache // lookup -func (tx *UniqueTx) Evict() { tx.unique = false } // Lock is already held here +func (tx *UniqueTx) Evict() { + // Lock is already held here + tx.unique = false + tx.deps = nil +} func (tx *UniqueTx) setStatus(status choices.Status) error { tx.refresh() @@ -206,22 +210,25 @@ func (tx *UniqueTx) Dependencies() []snowstorm.Tx { continue } txID, _ := in.InputSource() - if !txIDs.Contains(txID) { - txIDs.Add(txID) - tx.deps = append(tx.deps, &UniqueTx{ - vm: tx.vm, - txID: txID, - }) + if txIDs.Contains(txID) { + continue } + txIDs.Add(txID) + tx.deps = append(tx.deps, &UniqueTx{ + vm: tx.vm, + txID: txID, + }) } + consumedIDs := tx.Tx.ConsumedAssetIDs() for _, assetID := range tx.Tx.AssetIDs().List() { - if !txIDs.Contains(assetID) { - txIDs.Add(assetID) - tx.deps = append(tx.deps, &UniqueTx{ - vm: tx.vm, - txID: assetID, - }) + if consumedIDs.Contains(assetID) || txIDs.Contains(assetID) { + continue } + txIDs.Add(assetID) + tx.deps = append(tx.deps, &UniqueTx{ + vm: tx.vm, + txID: assetID, + }) } return tx.deps } diff --git a/vms/avm/vm.go b/vms/avm/vm.go index b7f7252..026760c 100644 --- a/vms/avm/vm.go +++ b/vms/avm/vm.go @@ -20,12 +20,12 @@ import ( "github.com/ava-labs/gecko/snow/choices" "github.com/ava-labs/gecko/snow/consensus/snowstorm" "github.com/ava-labs/gecko/snow/engine/common" + "github.com/ava-labs/gecko/utils/codec" "github.com/ava-labs/gecko/utils/formatting" "github.com/ava-labs/gecko/utils/logging" "github.com/ava-labs/gecko/utils/timer" "github.com/ava-labs/gecko/utils/wrappers" "github.com/ava-labs/gecko/vms/components/ava" - "github.com/ava-labs/gecko/vms/components/codec" cjson "github.com/ava-labs/gecko/utils/json" ) @@ -35,7 +35,7 @@ const ( batchSize = 30 stateCacheSize = 10000 idCacheSize = 10000 - txCacheSize = 100000 + txCacheSize = 10000 addressSep = "-" ) @@ -248,8 +248,8 @@ func (vm *VM) CreateHandlers() map[string]*common.HTTPHandler { rpcServer.RegisterService(&Service{vm: vm}, "avm") // name this service "avm" return map[string]*common.HTTPHandler{ - "": &common.HTTPHandler{Handler: rpcServer}, - "/pubsub": &common.HTTPHandler{LockOptions: common.NoLock, Handler: vm.pubsub}, + "": {Handler: rpcServer}, + "/pubsub": {LockOptions: common.NoLock, Handler: vm.pubsub}, } } @@ -261,7 +261,7 @@ func (vm *VM) CreateStaticHandlers() map[string]*common.HTTPHandler { newServer.RegisterCodec(codec, "application/json;charset=UTF-8") newServer.RegisterService(&StaticService{}, "avm") // name this service "avm" return map[string]*common.HTTPHandler{ - "": &common.HTTPHandler{LockOptions: common.WriteLock, Handler: newServer}, + "": {LockOptions: common.WriteLock, Handler: newServer}, } } @@ -492,10 +492,10 @@ func (vm *VM) parseTx(b []byte) (*UniqueTx, error) { if err := vm.state.SetTx(tx.ID(), tx.Tx); err != nil { return nil, err } - if err := tx.setStatus(choices.Processing); err != nil { return nil, err } + return tx, vm.db.Commit() } return tx, nil diff --git a/vms/avm/vm_test.go b/vms/avm/vm_test.go index d3a2d73..81a08f8 100644 --- a/vms/avm/vm_test.go +++ b/vms/avm/vm_test.go @@ -84,11 +84,11 @@ func BuildGenesisTest(t *testing.T) []byte { addr2 := keys[2].PublicKey().Address() args := BuildGenesisArgs{GenesisData: map[string]AssetDefinition{ - "asset1": AssetDefinition{ + "asset1": { Name: "myFixedCapAsset", Symbol: "MFCA", InitialState: map[string][]interface{}{ - "fixedCap": []interface{}{ + "fixedCap": { Holder{ Amount: 100000, Address: addr0.String(), @@ -108,11 +108,11 @@ func BuildGenesisTest(t *testing.T) []byte { }, }, }, - "asset2": AssetDefinition{ + "asset2": { Name: "myVarCapAsset", Symbol: "MVCA", InitialState: map[string][]interface{}{ - "variableCap": []interface{}{ + "variableCap": { Owners{ Threshold: 1, Minters: []string{ @@ -131,10 +131,10 @@ func BuildGenesisTest(t *testing.T) []byte { }, }, }, - "asset3": AssetDefinition{ + "asset3": { Name: "myOtherVarCapAsset", InitialState: map[string][]interface{}{ - "variableCap": []interface{}{ + "variableCap": { Owners{ Threshold: 1, Minters: []string{ @@ -168,7 +168,7 @@ func GenesisVM(t *testing.T) ([]byte, chan common.Message, *VM) { memdb.New(), genesisBytes, issuer, - []*common.Fx{&common.Fx{ + []*common.Fx{{ ID: ids.Empty, Fx: &secp256k1fx.Fx{}, }}, @@ -195,7 +195,7 @@ func NewTx(t *testing.T, genesisBytes []byte, vm *VM) *Tx { newTx := &Tx{UnsignedTx: &BaseTx{ NetID: networkID, BCID: chainID, - Ins: []*ava.TransferableInput{&ava.TransferableInput{ + Ins: []*ava.TransferableInput{{ UTXOID: ava.UTXOID{ TxID: genesisTx.ID(), OutputIndex: 1, @@ -357,7 +357,7 @@ func TestTxSerialization(t *testing.T) { Symbol: "symb", Denomination: 0, States: []*InitialState{ - &InitialState{ + { FxID: 0, Outs: []verify.Verifiable{ &secp256k1fx.MintOutput{ @@ -456,7 +456,7 @@ func TestFxInitializationFailure(t *testing.T) { /*db=*/ memdb.New(), /*genesisState=*/ genesisBytes, /*engineMessenger=*/ make(chan common.Message, 1), - /*fxs=*/ []*common.Fx{&common.Fx{ + /*fxs=*/ []*common.Fx{{ ID: ids.Empty, Fx: &testFx{initialize: errUnknownFx}, }}, @@ -537,7 +537,7 @@ func TestIssueDependentTx(t *testing.T) { firstTx := &Tx{UnsignedTx: &BaseTx{ NetID: networkID, BCID: chainID, - Ins: []*ava.TransferableInput{&ava.TransferableInput{ + Ins: []*ava.TransferableInput{{ UTXOID: ava.UTXOID{ TxID: genesisTx.ID(), OutputIndex: 1, @@ -552,7 +552,7 @@ func TestIssueDependentTx(t *testing.T) { }, }, }}, - Outs: []*ava.TransferableOutput{&ava.TransferableOutput{ + Outs: []*ava.TransferableOutput{{ Asset: ava.Asset{ID: genesisTx.ID()}, Out: &secp256k1fx.TransferOutput{ Amt: 50000, @@ -596,7 +596,7 @@ func TestIssueDependentTx(t *testing.T) { secondTx := &Tx{UnsignedTx: &BaseTx{ NetID: networkID, BCID: chainID, - Ins: []*ava.TransferableInput{&ava.TransferableInput{ + Ins: []*ava.TransferableInput{{ UTXOID: ava.UTXOID{ TxID: firstTx.ID(), OutputIndex: 0, @@ -671,11 +671,11 @@ func TestIssueNFT(t *testing.T) { genesisBytes, issuer, []*common.Fx{ - &common.Fx{ + { ID: ids.Empty.Prefix(0), Fx: &secp256k1fx.Fx{}, }, - &common.Fx{ + { ID: ids.Empty.Prefix(1), Fx: &nftfx.Fx{}, }, @@ -704,7 +704,7 @@ func TestIssueNFT(t *testing.T) { Name: "Team Rocket", Symbol: "TR", Denomination: 0, - States: []*InitialState{&InitialState{ + States: []*InitialState{{ FxID: 1, Outs: []verify.Verifiable{ &nftfx.MintOutput{ @@ -740,9 +740,9 @@ func TestIssueNFT(t *testing.T) { NetID: networkID, BCID: chainID, }, - Ops: []*Operation{&Operation{ + Ops: []*Operation{{ Asset: ava.Asset{ID: createAssetTx.ID()}, - UTXOIDs: []*ava.UTXOID{&ava.UTXOID{ + UTXOIDs: []*ava.UTXOID{{ TxID: createAssetTx.ID(), OutputIndex: 0, }}, @@ -752,9 +752,7 @@ func TestIssueNFT(t *testing.T) { }, GroupID: 1, Payload: []byte{'h', 'e', 'l', 'l', 'o'}, - Outputs: []*secp256k1fx.OutputOwners{ - &secp256k1fx.OutputOwners{}, - }, + Outputs: []*secp256k1fx.OutputOwners{{}}, }, }}, }} @@ -793,9 +791,9 @@ func TestIssueNFT(t *testing.T) { NetID: networkID, BCID: chainID, }, - Ops: []*Operation{&Operation{ + Ops: []*Operation{{ Asset: ava.Asset{ID: createAssetTx.ID()}, - UTXOIDs: []*ava.UTXOID{&ava.UTXOID{ + UTXOIDs: []*ava.UTXOID{{ TxID: mintNFTTx.ID(), OutputIndex: 0, }}, @@ -840,15 +838,15 @@ func TestIssueProperty(t *testing.T) { genesisBytes, issuer, []*common.Fx{ - &common.Fx{ + { ID: ids.Empty.Prefix(0), Fx: &secp256k1fx.Fx{}, }, - &common.Fx{ + { ID: ids.Empty.Prefix(1), Fx: &nftfx.Fx{}, }, - &common.Fx{ + { ID: ids.Empty.Prefix(2), Fx: &propertyfx.Fx{}, }, @@ -877,7 +875,7 @@ func TestIssueProperty(t *testing.T) { Name: "Team Rocket", Symbol: "TR", Denomination: 0, - States: []*InitialState{&InitialState{ + States: []*InitialState{{ FxID: 2, Outs: []verify.Verifiable{ &propertyfx.MintOutput{ @@ -905,9 +903,9 @@ func TestIssueProperty(t *testing.T) { NetID: networkID, BCID: chainID, }, - Ops: []*Operation{&Operation{ + Ops: []*Operation{{ Asset: ava.Asset{ID: createAssetTx.ID()}, - UTXOIDs: []*ava.UTXOID{&ava.UTXOID{ + UTXOIDs: []*ava.UTXOID{{ TxID: createAssetTx.ID(), OutputIndex: 0, }}, @@ -960,9 +958,9 @@ func TestIssueProperty(t *testing.T) { NetID: networkID, BCID: chainID, }, - Ops: []*Operation{&Operation{ + Ops: []*Operation{{ Asset: ava.Asset{ID: createAssetTx.ID()}, - UTXOIDs: []*ava.UTXOID{&ava.UTXOID{ + UTXOIDs: []*ava.UTXOID{{ TxID: mintPropertyTx.ID(), OutputIndex: 1, }}, diff --git a/vms/components/ava/asset_test.go b/vms/components/ava/asset_test.go index 40d6ea8..79ae7d5 100644 --- a/vms/components/ava/asset_test.go +++ b/vms/components/ava/asset_test.go @@ -7,7 +7,7 @@ import ( "testing" "github.com/ava-labs/gecko/ids" - "github.com/ava-labs/gecko/vms/components/codec" + "github.com/ava-labs/gecko/utils/codec" ) func TestAssetVerifyNil(t *testing.T) { diff --git a/vms/components/ava/prefixed_state.go b/vms/components/ava/prefixed_state.go index 92b3491..9906381 100644 --- a/vms/components/ava/prefixed_state.go +++ b/vms/components/ava/prefixed_state.go @@ -9,7 +9,7 @@ import ( "github.com/ava-labs/gecko/ids" "github.com/ava-labs/gecko/snow/choices" "github.com/ava-labs/gecko/utils/hashing" - "github.com/ava-labs/gecko/vms/components/codec" + "github.com/ava-labs/gecko/utils/codec" ) // Addressable is the interface a feature extension must provide to be able to diff --git a/vms/components/ava/prefixed_state_test.go b/vms/components/ava/prefixed_state_test.go index 06cb1df..d3019d5 100644 --- a/vms/components/ava/prefixed_state_test.go +++ b/vms/components/ava/prefixed_state_test.go @@ -9,7 +9,7 @@ import ( "github.com/ava-labs/gecko/database/memdb" "github.com/ava-labs/gecko/ids" "github.com/ava-labs/gecko/utils/hashing" - "github.com/ava-labs/gecko/vms/components/codec" + "github.com/ava-labs/gecko/utils/codec" "github.com/stretchr/testify/assert" ) diff --git a/vms/components/ava/state.go b/vms/components/ava/state.go index fc3b929..df724a4 100644 --- a/vms/components/ava/state.go +++ b/vms/components/ava/state.go @@ -11,7 +11,7 @@ import ( "github.com/ava-labs/gecko/database/prefixdb" "github.com/ava-labs/gecko/ids" "github.com/ava-labs/gecko/snow/choices" - "github.com/ava-labs/gecko/vms/components/codec" + "github.com/ava-labs/gecko/utils/codec" ) var ( diff --git a/vms/components/ava/transferables.go b/vms/components/ava/transferables.go index 4aa906d..85c2414 100644 --- a/vms/components/ava/transferables.go +++ b/vms/components/ava/transferables.go @@ -10,7 +10,7 @@ import ( "github.com/ava-labs/gecko/utils" "github.com/ava-labs/gecko/utils/crypto" - "github.com/ava-labs/gecko/vms/components/codec" + "github.com/ava-labs/gecko/utils/codec" "github.com/ava-labs/gecko/vms/components/verify" ) diff --git a/vms/components/ava/transferables_test.go b/vms/components/ava/transferables_test.go index 80205a6..08d7b69 100644 --- a/vms/components/ava/transferables_test.go +++ b/vms/components/ava/transferables_test.go @@ -9,7 +9,7 @@ import ( "github.com/ava-labs/gecko/ids" "github.com/ava-labs/gecko/utils/formatting" - "github.com/ava-labs/gecko/vms/components/codec" + "github.com/ava-labs/gecko/utils/codec" "github.com/ava-labs/gecko/vms/secp256k1fx" ) diff --git a/vms/components/ava/utxo_id_test.go b/vms/components/ava/utxo_id_test.go index 7944961..d1be00f 100644 --- a/vms/components/ava/utxo_id_test.go +++ b/vms/components/ava/utxo_id_test.go @@ -7,7 +7,7 @@ import ( "testing" "github.com/ava-labs/gecko/ids" - "github.com/ava-labs/gecko/vms/components/codec" + "github.com/ava-labs/gecko/utils/codec" ) func TestUTXOIDVerifyNil(t *testing.T) { diff --git a/vms/components/ava/utxo_test.go b/vms/components/ava/utxo_test.go index 07b067a..151e219 100644 --- a/vms/components/ava/utxo_test.go +++ b/vms/components/ava/utxo_test.go @@ -9,7 +9,7 @@ import ( "github.com/ava-labs/gecko/ids" "github.com/ava-labs/gecko/utils/formatting" - "github.com/ava-labs/gecko/vms/components/codec" + "github.com/ava-labs/gecko/utils/codec" "github.com/ava-labs/gecko/vms/secp256k1fx" ) diff --git a/vms/components/codec/codec.go b/vms/components/codec/codec.go deleted file mode 100644 index 72192cb..0000000 --- a/vms/components/codec/codec.go +++ /dev/null @@ -1,345 +0,0 @@ -// (c) 2019-2020, Ava Labs, Inc. All rights reserved. -// See the file LICENSE for licensing terms. - -package codec - -import ( - "errors" - "fmt" - "reflect" - "unicode" - - "github.com/ava-labs/gecko/utils/wrappers" -) - -const ( - defaultMaxSize = 1 << 18 // default max size, in bytes, of something being marshalled by Marshal() - defaultMaxSliceLength = 1 << 18 // default max length of a slice being marshalled by Marshal() -) - -// ErrBadCodec is returned when one tries to perform an operation -// using an unknown codec -var ( - errBadCodec = errors.New("wrong or unknown codec used") - errNil = errors.New("can't marshal nil value") - errUnmarshalNil = errors.New("can't unmarshal into nil") - errNeedPointer = errors.New("must unmarshal into a pointer") - errMarshalUnregisteredType = errors.New("can't marshal an unregistered type") - errUnmarshalUnregisteredType = errors.New("can't unmarshal an unregistered type") - errUnknownType = errors.New("don't know how to marshal/unmarshal this type") - errMarshalUnexportedField = errors.New("can't serialize an unexported field") - errUnmarshalUnexportedField = errors.New("can't deserialize into an unexported field") - errOutOfMemory = errors.New("out of memory") - errSliceTooLarge = errors.New("slice too large") -) - -// Codec handles marshaling and unmarshaling of structs -type codec struct { - maxSize int - maxSliceLen int - - typeIDToType map[uint32]reflect.Type - typeToTypeID map[reflect.Type]uint32 -} - -// Codec marshals and unmarshals -type Codec interface { - RegisterType(interface{}) error - Marshal(interface{}) ([]byte, error) - Unmarshal([]byte, interface{}) error -} - -// New returns a new codec -func New(maxSize, maxSliceLen int) Codec { - return codec{ - maxSize: maxSize, - maxSliceLen: maxSliceLen, - typeIDToType: map[uint32]reflect.Type{}, - typeToTypeID: map[reflect.Type]uint32{}, - } -} - -// NewDefault returns a new codec with reasonable default values -func NewDefault() Codec { return New(defaultMaxSize, defaultMaxSliceLength) } - -// RegisterType is used to register types that may be unmarshaled into an interface typed value -// [val] is a value of the type being registered -func (c codec) RegisterType(val interface{}) error { - valType := reflect.TypeOf(val) - if _, exists := c.typeToTypeID[valType]; exists { - return fmt.Errorf("type %v has already been registered", valType) - } - c.typeIDToType[uint32(len(c.typeIDToType))] = reflect.TypeOf(val) - c.typeToTypeID[valType] = uint32(len(c.typeIDToType) - 1) - return nil -} - -// A few notes: -// 1) See codec_test.go for examples of usage -// 2) We use "marshal" and "serialize" interchangeably, and "unmarshal" and "deserialize" interchangeably -// 3) To include a field of a struct in the serialized form, add the tag `serialize:"true"` to it -// 4) These typed members of a struct may be serialized: -// bool, string, uint[8,16,32,64, int[8,16,32,64], -// structs, slices, arrays, interface. -// structs, slices and arrays can only be serialized if their constituent parts can be. -// 5) To marshal an interface typed value, you must pass a _pointer_ to the value -// 6) If you want to be able to unmarshal into an interface typed value, -// you must call codec.RegisterType([instance of the type that fulfills the interface]). -// 7) nil slices will be unmarshaled as an empty slice of the appropriate type -// 8) Serialized fields must be exported - -// Marshal returns the byte representation of [value] -// If you want to marshal an interface, [value] must be a pointer -// to the interface -func (c codec) Marshal(value interface{}) ([]byte, error) { - if value == nil { - return nil, errNil - } - - return c.marshal(reflect.ValueOf(value)) -} - -// Marshal [value] to bytes -func (c codec) marshal(value reflect.Value) ([]byte, error) { - p := wrappers.Packer{MaxSize: c.maxSize, Bytes: []byte{}} - t := value.Type() - - valueKind := value.Kind() - switch valueKind { - case reflect.Interface, reflect.Ptr, reflect.Slice: - if value.IsNil() { - return nil, errNil - } - } - - switch valueKind { - case reflect.Uint8: - p.PackByte(uint8(value.Uint())) - return p.Bytes, p.Err - case reflect.Int8: - p.PackByte(uint8(value.Int())) - return p.Bytes, p.Err - case reflect.Uint16: - p.PackShort(uint16(value.Uint())) - return p.Bytes, p.Err - case reflect.Int16: - p.PackShort(uint16(value.Int())) - return p.Bytes, p.Err - case reflect.Uint32: - p.PackInt(uint32(value.Uint())) - return p.Bytes, p.Err - case reflect.Int32: - p.PackInt(uint32(value.Int())) - return p.Bytes, p.Err - case reflect.Uint64: - p.PackLong(value.Uint()) - return p.Bytes, p.Err - case reflect.Int64: - p.PackLong(uint64(value.Int())) - return p.Bytes, p.Err - case reflect.Uintptr, reflect.Ptr: - return c.marshal(value.Elem()) - case reflect.String: - p.PackStr(value.String()) - return p.Bytes, p.Err - case reflect.Bool: - p.PackBool(value.Bool()) - return p.Bytes, p.Err - case reflect.Interface: - typeID, ok := c.typeToTypeID[reflect.TypeOf(value.Interface())] // Get the type ID of the value being marshaled - if !ok { - return nil, fmt.Errorf("can't marshal unregistered type '%v'", reflect.TypeOf(value.Interface()).String()) - } - p.PackInt(typeID) - bytes, err := c.Marshal(value.Interface()) - if err != nil { - return nil, err - } - p.PackFixedBytes(bytes) - if p.Errored() { - return nil, p.Err - } - return p.Bytes, err - case reflect.Array, reflect.Slice: - numElts := value.Len() // # elements in the slice/array (assumed to be <= 2^31 - 1) - // If this is a slice, pack the number of elements in the slice - if valueKind == reflect.Slice { - p.PackInt(uint32(numElts)) - } - for i := 0; i < numElts; i++ { // Pack each element in the slice/array - eltBytes, err := c.marshal(value.Index(i)) - if err != nil { - return nil, err - } - p.PackFixedBytes(eltBytes) - } - return p.Bytes, p.Err - case reflect.Struct: - for i := 0; i < t.NumField(); i++ { // Go through all fields of this struct - field := t.Field(i) - if !shouldSerialize(field) { // Skip fields we don't need to serialize - continue - } - if unicode.IsLower(rune(field.Name[0])) { // Can only marshal exported fields - return nil, errMarshalUnexportedField - } - fieldVal := value.Field(i) // The field we're serializing - if fieldVal.Kind() == reflect.Slice && fieldVal.IsNil() { - p.PackInt(0) - continue - } - fieldBytes, err := c.marshal(fieldVal) // Serialize the field - if err != nil { - return nil, err - } - p.PackFixedBytes(fieldBytes) - } - return p.Bytes, p.Err - case reflect.Invalid: - return nil, errUnmarshalNil - default: - return nil, errUnknownType - } -} - -// Unmarshal unmarshals [bytes] into [dest], where -// [dest] must be a pointer or interface -func (c codec) Unmarshal(bytes []byte, dest interface{}) error { - p := &wrappers.Packer{Bytes: bytes} - - if len(bytes) > c.maxSize { - return errSliceTooLarge - } - - if dest == nil { - return errNil - } - - destPtr := reflect.ValueOf(dest) - - if destPtr.Kind() != reflect.Ptr { - return errNeedPointer - } - - destVal := destPtr.Elem() - - err := c.unmarshal(p, destVal) - if err != nil { - return err - } - - if p.Offset != len(p.Bytes) { - return fmt.Errorf("has %d leftover bytes after unmarshalling", len(p.Bytes)-p.Offset) - } - return nil -} - -// Unmarshal bytes from [p] into [field] -// [field] must be addressable -func (c codec) unmarshal(p *wrappers.Packer, field reflect.Value) error { - kind := field.Kind() - switch kind { - case reflect.Uint8: - field.SetUint(uint64(p.UnpackByte())) - case reflect.Int8: - field.SetInt(int64(p.UnpackByte())) - case reflect.Uint16: - field.SetUint(uint64(p.UnpackShort())) - case reflect.Int16: - field.SetInt(int64(p.UnpackShort())) - case reflect.Uint32: - field.SetUint(uint64(p.UnpackInt())) - case reflect.Int32: - field.SetInt(int64(p.UnpackInt())) - case reflect.Uint64: - field.SetUint(p.UnpackLong()) - case reflect.Int64: - field.SetInt(int64(p.UnpackLong())) - case reflect.Bool: - field.SetBool(p.UnpackBool()) - case reflect.Slice: - sliceLen := int(p.UnpackInt()) // number of elements in the slice - if sliceLen < 0 || sliceLen > c.maxSliceLen { - return errSliceTooLarge - } - - // First set [field] to be a slice of the appropriate type/capacity (right now [field] is nil) - slice := reflect.MakeSlice(field.Type(), sliceLen, sliceLen) - field.Set(slice) - // Unmarshal each element into the appropriate index of the slice - for i := 0; i < sliceLen; i++ { - if err := c.unmarshal(p, field.Index(i)); err != nil { - return err - } - } - case reflect.Array: - for i := 0; i < field.Len(); i++ { - if err := c.unmarshal(p, field.Index(i)); err != nil { - return err - } - } - case reflect.String: - field.SetString(p.UnpackStr()) - case reflect.Interface: - // Get the type ID - typeID := p.UnpackInt() - // Get a struct that implements the interface - typ, ok := c.typeIDToType[typeID] - if !ok { - return errUnmarshalUnregisteredType - } - // Ensure struct actually does implement the interface - fieldType := field.Type() - if !typ.Implements(fieldType) { - return fmt.Errorf("%s does not implement interface %s", typ, fieldType) - } - concreteInstancePtr := reflect.New(typ) // instance of the proper type - // Unmarshal into the struct - if err := c.unmarshal(p, concreteInstancePtr.Elem()); err != nil { - return err - } - // And assign the filled struct to the field - field.Set(concreteInstancePtr.Elem()) - case reflect.Struct: - // Type of this struct - structType := reflect.TypeOf(field.Interface()) - // Go through all the fields and umarshal into each - for i := 0; i < structType.NumField(); i++ { - structField := structType.Field(i) - if !shouldSerialize(structField) { // Skip fields we don't need to unmarshal - continue - } - if unicode.IsLower(rune(structField.Name[0])) { // Only unmarshal into exported field - return errUnmarshalUnexportedField - } - field := field.Field(i) // Get the field - if err := c.unmarshal(p, field); err != nil { // Unmarshal into the field - return err - } - if p.Errored() { // If there was an error just return immediately - return p.Err - } - } - case reflect.Ptr: - // Get the type this pointer points to - underlyingType := field.Type().Elem() - // Create a new pointer to a new value of the underlying type - underlyingValue := reflect.New(underlyingType) - // Fill the value - if err := c.unmarshal(p, underlyingValue.Elem()); err != nil { - return err - } - // Assign to the top-level struct's member - field.Set(underlyingValue) - case reflect.Invalid: - return errUnmarshalNil - default: - return errUnknownType - } - return p.Err -} - -// Returns true iff [field] should be serialized -func shouldSerialize(field reflect.StructField) bool { - return field.Tag.Get("serialize") == "true" -} diff --git a/vms/components/core/block.go b/vms/components/core/block.go index 3609477..6d1d37b 100644 --- a/vms/components/core/block.go +++ b/vms/components/core/block.go @@ -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 diff --git a/vms/components/core/block_test.go b/vms/components/core/block_test.go new file mode 100644 index 0000000..d9d30bc --- /dev/null +++ b/vms/components/core/block_test.go @@ -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) + } +} diff --git a/vms/components/state/state.go b/vms/components/state/state.go index d4d0da8..24b50e3 100644 --- a/vms/components/state/state.go +++ b/vms/components/state/state.go @@ -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 diff --git a/vms/nftfx/fx_test.go b/vms/nftfx/fx_test.go index d965902..0cfbd87 100644 --- a/vms/nftfx/fx_test.go +++ b/vms/nftfx/fx_test.go @@ -9,7 +9,7 @@ import ( "github.com/ava-labs/gecko/utils/hashing" "github.com/ava-labs/gecko/utils/logging" "github.com/ava-labs/gecko/utils/timer" - "github.com/ava-labs/gecko/vms/components/codec" + "github.com/ava-labs/gecko/utils/codec" "github.com/ava-labs/gecko/vms/secp256k1fx" ) diff --git a/vms/platformvm/add_default_subnet_delegator_tx_test.go b/vms/platformvm/add_default_subnet_delegator_tx_test.go index 65a0a71..4b6fe80 100644 --- a/vms/platformvm/add_default_subnet_delegator_tx_test.go +++ b/vms/platformvm/add_default_subnet_delegator_tx_test.go @@ -335,9 +335,9 @@ func TestAddDefaultSubnetDelegatorTxSemanticVerify(t *testing.T) { } tx, err = vm.newAddDefaultSubnetDelegatorTx( - defaultNonce+1, // nonce - defaultStakeAmount, // weight - uint64(newTimestamp.Unix()), // start time + defaultNonce+1, // nonce + defaultStakeAmount, // weight + uint64(newTimestamp.Unix()), // start time uint64(newTimestamp.Add(MinimumStakingDuration).Unix()), // end time defaultKey.PublicKey().Address(), // node ID defaultKey.PublicKey().Address(), // destination @@ -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") + // } } diff --git a/vms/platformvm/service.go b/vms/platformvm/service.go index de2d41b..9913608 100644 --- a/vms/platformvm/service.go +++ b/vms/platformvm/service.go @@ -234,7 +234,7 @@ type GetCurrentValidatorsReply struct { // GetCurrentValidators returns the list of current validators func (service *Service) GetCurrentValidators(_ *http.Request, args *GetCurrentValidatorsArgs, reply *GetCurrentValidatorsReply) error { - service.vm.Ctx.Log.Debug("GetCurrentValidators called") + service.vm.Ctx.Log.Info("Platform: GetCurrentValidators called") if args.SubnetID.IsZero() { args.SubnetID = DefaultSubnetID @@ -298,7 +298,7 @@ type GetPendingValidatorsReply struct { // GetPendingValidators returns the list of current validators func (service *Service) GetPendingValidators(_ *http.Request, args *GetPendingValidatorsArgs, reply *GetPendingValidatorsReply) error { - service.vm.Ctx.Log.Debug("GetPendingValidators called") + service.vm.Ctx.Log.Info("Platform: GetPendingValidators called") if args.SubnetID.IsZero() { args.SubnetID = DefaultSubnetID @@ -360,7 +360,7 @@ type SampleValidatorsReply struct { // SampleValidators returns a sampling of the list of current validators func (service *Service) SampleValidators(_ *http.Request, args *SampleValidatorsArgs, reply *SampleValidatorsReply) error { - service.vm.Ctx.Log.Debug("Sample called with {Size = %d}", args.Size) + service.vm.Ctx.Log.Info("Platform: SampleValidators called with {Size = %d}", args.Size) if args.SubnetID.IsZero() { args.SubnetID = DefaultSubnetID @@ -437,7 +437,7 @@ type ListAccountsReply struct { // ListAccounts lists all of the accounts controlled by [args.Username] func (service *Service) ListAccounts(_ *http.Request, args *ListAccountsArgs, reply *ListAccountsReply) error { - service.vm.Ctx.Log.Debug("listAccounts called for user '%s'", args.Username) + service.vm.Ctx.Log.Info("Platform: ListAccounts called for user '%s'", args.Username) // db holds the user's info that pertains to the Platform Chain userDB, err := service.vm.Ctx.Keystore.GetDatabase(args.Username, args.Password) @@ -499,7 +499,7 @@ type CreateAccountReply struct { // The account's ID is [privKey].PublicKey().Address(), where [privKey] is a // private key controlled by the user. func (service *Service) CreateAccount(_ *http.Request, args *CreateAccountArgs, reply *CreateAccountReply) error { - service.vm.Ctx.Log.Debug("createAccount called for user '%s'", args.Username) + service.vm.Ctx.Log.Info("Platform: CreateAccount called for user '%s'", args.Username) // userDB holds the user's info that pertains to the Platform Chain userDB, err := service.vm.Ctx.Keystore.GetDatabase(args.Username, args.Password) @@ -569,7 +569,7 @@ type AddDefaultSubnetValidatorArgs struct { // AddDefaultSubnetValidator returns an unsigned transaction to add a validator to the default subnet // The returned unsigned transaction should be signed using Sign() func (service *Service) AddDefaultSubnetValidator(_ *http.Request, args *AddDefaultSubnetValidatorArgs, reply *CreateTxResponse) error { - service.vm.Ctx.Log.Debug("AddDefaultSubnetValidator called") + service.vm.Ctx.Log.Info("Platform: AddDefaultSubnetValidator called") switch { case args.ID.IsZero(): // If ID unspecified, use this node's ID as validator ID @@ -626,7 +626,7 @@ type AddDefaultSubnetDelegatorArgs struct { // to the default subnet // The returned unsigned transaction should be signed using Sign() func (service *Service) AddDefaultSubnetDelegator(_ *http.Request, args *AddDefaultSubnetDelegatorArgs, reply *CreateTxResponse) error { - service.vm.Ctx.Log.Debug("AddDefaultSubnetDelegator called") + service.vm.Ctx.Log.Info("Platform: AddDefaultSubnetDelegator called") switch { case args.ID.IsZero(): // If ID unspecified, use this node's ID as validator ID @@ -741,7 +741,7 @@ type CreateSubnetArgs struct { // CreateSubnet returns an unsigned transaction to create a new subnet. // The unsigned transaction must be signed with the key of [args.Payer] func (service *Service) CreateSubnet(_ *http.Request, args *CreateSubnetArgs, response *CreateTxResponse) error { - service.vm.Ctx.Log.Debug("platform.createSubnet called") + service.vm.Ctx.Log.Info("Platform: CreateSubnet called") switch { case args.PayerNonce == 0: @@ -796,7 +796,7 @@ type ExportAVAArgs struct { // The unsigned transaction must be signed with the key of the account exporting the AVA // and paying the transaction fee func (service *Service) ExportAVA(_ *http.Request, args *ExportAVAArgs, response *CreateTxResponse) error { - service.vm.Ctx.Log.Debug("platform.ExportAVA called") + service.vm.Ctx.Log.Info("Platform: ExportAVA called") switch { case args.PayerNonce == 0: @@ -858,7 +858,7 @@ type SignResponse struct { // Sign [args.bytes] func (service *Service) Sign(_ *http.Request, args *SignArgs, reply *SignResponse) error { - service.vm.Ctx.Log.Debug("sign called") + service.vm.Ctx.Log.Info("Platform: Sign called") if args.Signer == "" { return errNilSigner @@ -938,7 +938,7 @@ func (service *Service) signAddDefaultSubnetValidatorTx(tx *addDefaultSubnetVali // Sign [unsigned] with [key] func (service *Service) signAddDefaultSubnetDelegatorTx(tx *addDefaultSubnetDelegatorTx, key *crypto.PrivateKeySECP256K1R) (*addDefaultSubnetDelegatorTx, error) { - service.vm.Ctx.Log.Debug("signAddDefaultSubnetValidatorTx called") + service.vm.Ctx.Log.Debug("signAddDefaultSubnetDelegatorTx called") // TODO: Should we check if tx is already signed? unsignedIntf := interface{}(&tx.UnsignedAddDefaultSubnetDelegatorTx) @@ -961,7 +961,7 @@ func (service *Service) signAddDefaultSubnetDelegatorTx(tx *addDefaultSubnetDele // Sign [xt] with [key] func (service *Service) signCreateSubnetTx(tx *CreateSubnetTx, key *crypto.PrivateKeySECP256K1R) (*CreateSubnetTx, error) { - service.vm.Ctx.Log.Debug("signAddDefaultSubnetValidatorTx called") + service.vm.Ctx.Log.Debug("signCreateSubnetTx called") // TODO: Should we check if tx is already signed? unsignedIntf := interface{}(&tx.UnsignedCreateSubnetTx) @@ -984,7 +984,7 @@ func (service *Service) signCreateSubnetTx(tx *CreateSubnetTx, key *crypto.Priva // Sign [tx] with [key] func (service *Service) signExportTx(tx *ExportTx, key *crypto.PrivateKeySECP256K1R) (*ExportTx, error) { - service.vm.Ctx.Log.Debug("platform.signAddDefaultSubnetValidatorTx called") + service.vm.Ctx.Log.Debug("signExportTx called") // TODO: Should we check if tx is already signed? unsignedIntf := interface{}(&tx.UnsignedExportTx) @@ -1075,7 +1075,7 @@ type ImportAVAArgs struct { // The AVA must have already been exported from the X-Chain. // The unsigned transaction must be signed with the key of the tx fee payer. func (service *Service) ImportAVA(_ *http.Request, args *ImportAVAArgs, response *SignResponse) error { - service.vm.Ctx.Log.Debug("platform.ImportAVA called") + service.vm.Ctx.Log.Info("Platform: ImportAVA called") switch { case args.To == "": @@ -1263,7 +1263,7 @@ type IssueTxResponse struct { // IssueTx issues the transaction [args.Tx] to the network func (service *Service) IssueTx(_ *http.Request, args *IssueTxArgs, response *IssueTxResponse) error { - service.vm.Ctx.Log.Debug("issueTx called") + service.vm.Ctx.Log.Info("Platform: IssueTx called") genTx := genericTx{} if err := Codec.Unmarshal(args.Tx.Bytes, &genTx); err != nil { @@ -1275,7 +1275,7 @@ func (service *Service) IssueTx(_ *http.Request, args *IssueTxArgs, response *Is if err := tx.initialize(service.vm); err != nil { return fmt.Errorf("error initializing tx: %s", err) } - service.vm.unissuedEvents.Push(tx) + service.vm.unissuedEvents.Add(tx) response.TxID = tx.ID() case DecisionTx: if err := tx.initialize(service.vm); err != nil { @@ -1290,7 +1290,7 @@ func (service *Service) IssueTx(_ *http.Request, args *IssueTxArgs, response *Is service.vm.unissuedAtomicTxs = append(service.vm.unissuedAtomicTxs, tx) response.TxID = tx.ID() default: - return errors.New("Could not parse given tx. Must be a TimedTx, DecisionTx, or AtomicTx") + return errors.New("Could not parse given tx. Provided tx needs to be a TimedTx, DecisionTx, or AtomicTx") } service.vm.resetTimer() @@ -1327,7 +1327,7 @@ type CreateBlockchainArgs struct { // CreateBlockchain returns an unsigned transaction to create a new blockchain // Must be signed with the Subnet's control keys and with a key that pays the transaction fee before issuance func (service *Service) CreateBlockchain(_ *http.Request, args *CreateBlockchainArgs, response *CreateTxResponse) error { - service.vm.Ctx.Log.Debug("createBlockchain called") + service.vm.Ctx.Log.Info("Platform: CreateBlockchain called") switch { case args.PayerNonce == 0: @@ -1410,7 +1410,7 @@ type GetBlockchainStatusReply struct { // GetBlockchainStatus gets the status of a blockchain with the ID [args.BlockchainID]. func (service *Service) GetBlockchainStatus(_ *http.Request, args *GetBlockchainStatusArgs, reply *GetBlockchainStatusReply) error { - service.vm.Ctx.Log.Debug("getBlockchainStatus called") + service.vm.Ctx.Log.Info("Platform: GetBlockchainStatus called") switch { case args.BlockchainID == "": @@ -1490,7 +1490,7 @@ type ValidatedByResponse struct { // ValidatedBy returns the ID of the Subnet that validates [args.BlockchainID] func (service *Service) ValidatedBy(_ *http.Request, args *ValidatedByArgs, response *ValidatedByResponse) error { - service.vm.Ctx.Log.Debug("validatedBy called") + service.vm.Ctx.Log.Info("Platform: ValidatedBy called") switch { case args.BlockchainID == "": @@ -1522,7 +1522,7 @@ type ValidatesResponse struct { // Validates returns the IDs of the blockchains validated by [args.SubnetID] func (service *Service) Validates(_ *http.Request, args *ValidatesArgs, response *ValidatesResponse) error { - service.vm.Ctx.Log.Debug("validates called") + service.vm.Ctx.Log.Info("Platform: Validates called") switch { case args.SubnetID == "": @@ -1576,7 +1576,7 @@ type GetBlockchainsResponse struct { // GetBlockchains returns all of the blockchains that exist func (service *Service) GetBlockchains(_ *http.Request, args *struct{}, response *GetBlockchainsResponse) error { - service.vm.Ctx.Log.Debug("getBlockchains called") + service.vm.Ctx.Log.Info("Platform: GetBlockchains called") chains, err := service.vm.getChains(service.vm.DB) if err != nil { diff --git a/vms/platformvm/service_test.go b/vms/platformvm/service_test.go index 9ac4a6c..b6e4a31 100644 --- a/vms/platformvm/service_test.go +++ b/vms/platformvm/service_test.go @@ -6,6 +6,9 @@ package platformvm import ( "encoding/json" "testing" + "time" + + "github.com/ava-labs/gecko/utils/formatting" ) func TestAddDefaultSubnetValidator(t *testing.T) { @@ -50,3 +53,184 @@ func TestImportKey(t *testing.T) { t.Fatal(err) } } + +func TestIssueTxKeepsTimedEventsSorted(t *testing.T) { + vm := defaultVM() + vm.Ctx.Lock.Lock() + defer func() { + vm.Shutdown() + vm.Ctx.Lock.Unlock() + }() + + service := Service{vm: vm} + + pendingValidatorStartTime1 := defaultGenesisTime.Add(3 * time.Second) + pendingValidatorEndTime1 := pendingValidatorStartTime1.Add(MinimumStakingDuration) + nodeIDKey1, _ := vm.factory.NewPrivateKey() + nodeID1 := nodeIDKey1.PublicKey().Address() + addPendingValidatorTx1, err := vm.newAddDefaultSubnetValidatorTx( + defaultNonce+1, + defaultStakeAmount, + uint64(pendingValidatorStartTime1.Unix()), + uint64(pendingValidatorEndTime1.Unix()), + nodeID1, + nodeID1, + NumberOfShares, + testNetworkID, + defaultKey, + ) + if err != nil { + t.Fatal(err) + } + + txBytes1, err := Codec.Marshal(genericTx{Tx: addPendingValidatorTx1}) + if err != nil { + t.Fatal(err) + } + + args1 := &IssueTxArgs{} + args1.Tx = formatting.CB58{Bytes: txBytes1} + reply1 := IssueTxResponse{} + + err = service.IssueTx(nil, args1, &reply1) + if err != nil { + t.Fatal(err) + } + + pendingValidatorStartTime2 := defaultGenesisTime.Add(2 * time.Second) + pendingValidatorEndTime2 := pendingValidatorStartTime2.Add(MinimumStakingDuration) + nodeIDKey2, _ := vm.factory.NewPrivateKey() + nodeID2 := nodeIDKey2.PublicKey().Address() + addPendingValidatorTx2, err := vm.newAddDefaultSubnetValidatorTx( + defaultNonce+1, + defaultStakeAmount, + uint64(pendingValidatorStartTime2.Unix()), + uint64(pendingValidatorEndTime2.Unix()), + nodeID2, + nodeID2, + NumberOfShares, + testNetworkID, + defaultKey, + ) + if err != nil { + t.Fatal(err) + } + + txBytes2, err := Codec.Marshal(genericTx{Tx: addPendingValidatorTx2}) + if err != nil { + t.Fatal(err) + } + + args2 := IssueTxArgs{Tx: formatting.CB58{Bytes: txBytes2}} + reply2 := IssueTxResponse{} + + err = service.IssueTx(nil, &args2, &reply2) + if err != nil { + t.Fatal(err) + } + + pendingValidatorStartTime3 := defaultGenesisTime.Add(10 * time.Second) + pendingValidatorEndTime3 := pendingValidatorStartTime3.Add(MinimumStakingDuration) + nodeIDKey3, _ := vm.factory.NewPrivateKey() + nodeID3 := nodeIDKey3.PublicKey().Address() + addPendingValidatorTx3, err := vm.newAddDefaultSubnetValidatorTx( + defaultNonce+1, + defaultStakeAmount, + uint64(pendingValidatorStartTime3.Unix()), + uint64(pendingValidatorEndTime3.Unix()), + nodeID3, + nodeID3, + NumberOfShares, + testNetworkID, + defaultKey, + ) + if err != nil { + t.Fatal(err) + } + + txBytes3, err := Codec.Marshal(genericTx{Tx: addPendingValidatorTx3}) + if err != nil { + t.Fatal(err) + } + + args3 := IssueTxArgs{Tx: formatting.CB58{Bytes: txBytes3}} + reply3 := IssueTxResponse{} + + err = service.IssueTx(nil, &args3, &reply3) + if err != nil { + t.Fatal(err) + } + + pendingValidatorStartTime4 := defaultGenesisTime.Add(1 * time.Second) + pendingValidatorEndTime4 := pendingValidatorStartTime4.Add(MinimumStakingDuration) + nodeIDKey4, _ := vm.factory.NewPrivateKey() + nodeID4 := nodeIDKey4.PublicKey().Address() + addPendingValidatorTx4, err := vm.newAddDefaultSubnetValidatorTx( + defaultNonce+1, + defaultStakeAmount, + uint64(pendingValidatorStartTime4.Unix()), + uint64(pendingValidatorEndTime4.Unix()), + nodeID4, + nodeID4, + NumberOfShares, + testNetworkID, + defaultKey, + ) + if err != nil { + t.Fatal(err) + } + + txBytes4, err := Codec.Marshal(genericTx{Tx: addPendingValidatorTx4}) + if err != nil { + t.Fatal(err) + } + + args4 := IssueTxArgs{Tx: formatting.CB58{Bytes: txBytes4}} + reply4 := IssueTxResponse{} + + err = service.IssueTx(nil, &args4, &reply4) + if err != nil { + t.Fatal(err) + } + + pendingValidatorStartTime5 := defaultGenesisTime.Add(50 * time.Second) + pendingValidatorEndTime5 := pendingValidatorStartTime5.Add(MinimumStakingDuration) + nodeIDKey5, _ := vm.factory.NewPrivateKey() + nodeID5 := nodeIDKey5.PublicKey().Address() + addPendingValidatorTx5, err := vm.newAddDefaultSubnetValidatorTx( + defaultNonce+1, + defaultStakeAmount, + uint64(pendingValidatorStartTime5.Unix()), + uint64(pendingValidatorEndTime5.Unix()), + nodeID5, + nodeID5, + NumberOfShares, + testNetworkID, + defaultKey, + ) + if err != nil { + t.Fatal(err) + } + + txBytes5, err := Codec.Marshal(genericTx{Tx: addPendingValidatorTx5}) + if err != nil { + t.Fatal(err) + } + + args5 := IssueTxArgs{Tx: formatting.CB58{Bytes: txBytes5}} + reply5 := IssueTxResponse{} + + err = service.IssueTx(nil, &args5, &reply5) + if err != nil { + t.Fatal(err) + } + + currentEvent := vm.unissuedEvents.Remove() + for vm.unissuedEvents.Len() > 0 { + nextEvent := vm.unissuedEvents.Remove() + if !currentEvent.StartTime().Before(nextEvent.StartTime()) { + t.Fatal("IssueTx does not keep event heap ordered") + } + currentEvent = nextEvent + } +} diff --git a/vms/platformvm/static_service.go b/vms/platformvm/static_service.go index 8acc0a9..1cdeeca 100644 --- a/vms/platformvm/static_service.go +++ b/vms/platformvm/static_service.go @@ -4,7 +4,6 @@ package platformvm import ( - "container/heap" "errors" "net/http" @@ -174,8 +173,8 @@ func (*StaticService) BuildGenesis(_ *http.Request, args *BuildGenesisArgs, repl return errAccountHasNoValue } accounts = append(accounts, newAccount( - account.Address, // ID - 0, // nonce + account.Address, // ID + 0, // nonce uint64(account.Balance), // balance )) } @@ -210,7 +209,7 @@ func (*StaticService) BuildGenesis(_ *http.Request, args *BuildGenesisArgs, repl return err } - heap.Push(validators, tx) + validators.Add(tx) } // Specify the chains that exist at genesis. diff --git a/vms/platformvm/static_service_test.go b/vms/platformvm/static_service_test.go index 04433ff..3f64a9b 100644 --- a/vms/platformvm/static_service_test.go +++ b/vms/platformvm/static_service_test.go @@ -111,3 +111,77 @@ func TestBuildGenesisInvalidEndtime(t *testing.T) { t.Fatalf("Should have errored due to an invalid end time") } } + +func TestBuildGenesisReturnsSortedValidators(t *testing.T) { + id := ids.NewShortID([20]byte{1}) + account := APIAccount{ + Address: id, + Balance: 123456789, + } + + weight := json.Uint64(987654321) + validator1 := APIDefaultSubnetValidator{ + APIValidator: APIValidator{ + StartTime: 0, + EndTime: 20, + Weight: &weight, + ID: id, + }, + Destination: id, + } + + validator2 := APIDefaultSubnetValidator{ + APIValidator: APIValidator{ + StartTime: 3, + EndTime: 15, + Weight: &weight, + ID: id, + }, + Destination: id, + } + + validator3 := APIDefaultSubnetValidator{ + APIValidator: APIValidator{ + StartTime: 1, + EndTime: 10, + Weight: &weight, + ID: id, + }, + Destination: id, + } + + args := BuildGenesisArgs{ + Accounts: []APIAccount{ + account, + }, + Validators: []APIDefaultSubnetValidator{ + validator1, + validator2, + validator3, + }, + Time: 5, + } + reply := BuildGenesisReply{} + + ss := StaticService{} + if err := ss.BuildGenesis(nil, &args, &reply); err != nil { + t.Fatalf("BuildGenesis should not have errored") + } + + genesis := &Genesis{} + if err := Codec.Unmarshal(reply.Bytes.Bytes, genesis); err != nil { + t.Fatal(err) + } + validators := genesis.Validators + if validators.Len() == 0 { + t.Fatal("Validators should contain 3 validators") + } + currentValidator := validators.Remove() + for validators.Len() > 0 { + nextValidator := validators.Remove() + if currentValidator.EndTime().Unix() > nextValidator.EndTime().Unix() { + t.Fatalf("Validators returned by genesis should be a min heap sorted by end time") + } + currentValidator = nextValidator + } +} diff --git a/vms/platformvm/vm.go b/vms/platformvm/vm.go index 9f1ce53..8b9350f 100644 --- a/vms/platformvm/vm.go +++ b/vms/platformvm/vm.go @@ -4,7 +4,6 @@ package platformvm import ( - "container/heap" "errors" "fmt" "time" @@ -19,6 +18,7 @@ import ( "github.com/ava-labs/gecko/snow/consensus/snowman" "github.com/ava-labs/gecko/snow/engine/common" "github.com/ava-labs/gecko/snow/validators" + "github.com/ava-labs/gecko/utils/codec" "github.com/ava-labs/gecko/utils/crypto" "github.com/ava-labs/gecko/utils/formatting" "github.com/ava-labs/gecko/utils/logging" @@ -27,7 +27,6 @@ import ( "github.com/ava-labs/gecko/utils/units" "github.com/ava-labs/gecko/utils/wrappers" "github.com/ava-labs/gecko/vms/components/ava" - "github.com/ava-labs/gecko/vms/components/codec" "github.com/ava-labs/gecko/vms/components/core" "github.com/ava-labs/gecko/vms/secp256k1fx" ) @@ -406,10 +405,14 @@ func (vm *VM) createChain(tx *CreateChainTx) { } // Bootstrapping marks this VM as bootstrapping -func (vm *VM) Bootstrapping() error { return nil } +func (vm *VM) Bootstrapping() error { + return vm.fx.Bootstrapping() +} // Bootstrapped marks this VM as bootstrapped -func (vm *VM) Bootstrapped() error { return nil } +func (vm *VM) Bootstrapped() error { + return vm.fx.Bootstrapped() +} // Shutdown this blockchain func (vm *VM) Shutdown() error { @@ -698,7 +701,7 @@ func (vm *VM) resetTimer() { vm.SnowmanVM.NotifyBlockReady() // Should issue a ProposeAddValidator return } - // If the tx doesn't meet the syncrony bound, drop it + // If the tx doesn't meet the synchrony bound, drop it vm.unissuedEvents.Remove() vm.Ctx.Log.Debug("dropping tx to add validator because its start time has passed") } @@ -780,8 +783,8 @@ func (vm *VM) calculateValidators(db database.Database, timestamp time.Time, sub if timestamp.Before(nextTx.StartTime()) { break } - heap.Push(current, nextTx) - heap.Pop(pending) + current.Add(nextTx) + pending.Remove() started.Add(nextTx.Vdr().ID()) } return current, pending, started, stopped, nil @@ -805,9 +808,11 @@ func (vm *VM) getValidators(validatorEvents *EventHeap) []validators.Validator { validator.Wght = weight } - vdrList := make([]validators.Validator, len(vdrMap))[:0] + vdrList := make([]validators.Validator, len(vdrMap)) + i := 0 for _, validator := range vdrMap { - vdrList = append(vdrList, validator) + vdrList[i] = validator + i++ } return vdrList } diff --git a/vms/platformvm/vm_test.go b/vms/platformvm/vm_test.go index b8bb47c..82989eb 100644 --- a/vms/platformvm/vm_test.go +++ b/vms/platformvm/vm_test.go @@ -5,7 +5,6 @@ package platformvm import ( "bytes" - "container/heap" "errors" "testing" "time" @@ -138,7 +137,7 @@ func defaultVM() *VM { vm.validators.PutValidatorSet(DefaultSubnetID, defaultSubnet) vm.clock.Set(defaultGenesisTime) - db := memdb.New() + db := prefixdb.New([]byte{0}, memdb.New()) msgChan := make(chan common.Message, 1) ctx := defaultContext() ctx.Lock.Lock() @@ -193,6 +192,8 @@ func defaultVM() *VM { panic("no subnets found") } // end delete + vm.registerDBTypes() + return vm } @@ -226,7 +227,7 @@ func GenesisCurrentValidators() *EventHeap { testNetworkID, // network ID key, // key paying tx fee and stake ) - heap.Push(validators, validator) + validators.Add(validator) } return validators } @@ -1011,7 +1012,7 @@ func TestCreateSubnet(t *testing.T) { t.Fatal(err) } - vm.unissuedEvents.Push(addValidatorTx) + vm.unissuedEvents.Add(addValidatorTx) blk, err = vm.BuildBlock() // should add validator to the new subnet if err != nil { t.Fatal(err) @@ -1188,7 +1189,7 @@ func TestAtomicImport(t *testing.T) { key := keys[0] sm := &atomic.SharedMemory{} - sm.Initialize(logging.NoLog{}, memdb.New()) + sm.Initialize(logging.NoLog{}, prefixdb.New([]byte{0}, vm.DB.GetDatabase())) vm.Ctx.SharedMemory = sm.NewBlockchainSharedMemory(vm.Ctx.ChainID) @@ -1281,7 +1282,7 @@ func TestOptimisticAtomicImport(t *testing.T) { key := keys[0] sm := &atomic.SharedMemory{} - sm.Initialize(logging.NoLog{}, memdb.New()) + sm.Initialize(logging.NoLog{}, prefixdb.New([]byte{0}, vm.DB.GetDatabase())) vm.Ctx.SharedMemory = sm.NewBlockchainSharedMemory(vm.Ctx.ChainID) diff --git a/vms/propertyfx/fx_test.go b/vms/propertyfx/fx_test.go index cfdf5c9..887cf73 100644 --- a/vms/propertyfx/fx_test.go +++ b/vms/propertyfx/fx_test.go @@ -9,7 +9,7 @@ import ( "github.com/ava-labs/gecko/utils/hashing" "github.com/ava-labs/gecko/utils/logging" "github.com/ava-labs/gecko/utils/timer" - "github.com/ava-labs/gecko/vms/components/codec" + "github.com/ava-labs/gecko/utils/codec" "github.com/ava-labs/gecko/vms/secp256k1fx" ) diff --git a/vms/rpcchainvm/factory.go b/vms/rpcchainvm/factory.go index a48bb01..5b2c2ce 100644 --- a/vms/rpcchainvm/factory.go +++ b/vms/rpcchainvm/factory.go @@ -5,11 +5,12 @@ package rpcchainvm import ( "errors" - "os/exec" - - "github.com/hashicorp/go-plugin" - "github.com/ava-labs/gecko/snow" + "github.com/hashicorp/go-hclog" + "github.com/hashicorp/go-plugin" + "io/ioutil" + "log" + "os/exec" ) var ( @@ -31,9 +32,18 @@ func (f *Factory) New(ctx *snow.Context) (interface{}, error) { }, } if ctx != nil { + log.SetOutput(ctx.Log) config.Stderr = ctx.Log - config.SyncStdout = ctx.Log - config.SyncStderr = ctx.Log + config.Logger = hclog.New(&hclog.LoggerOptions{ + Output: ctx.Log, + Level: hclog.Info, + }) + } else { + log.SetOutput(ioutil.Discard) + config.Stderr = ioutil.Discard + config.Logger = hclog.New(&hclog.LoggerOptions{ + Output: ioutil.Discard, + }) } client := plugin.NewClient(config) diff --git a/vms/secp256k1fx/credential_test.go b/vms/secp256k1fx/credential_test.go index 5157fab..e85ce1b 100644 --- a/vms/secp256k1fx/credential_test.go +++ b/vms/secp256k1fx/credential_test.go @@ -8,7 +8,7 @@ import ( "testing" "github.com/ava-labs/gecko/utils/crypto" - "github.com/ava-labs/gecko/vms/components/codec" + "github.com/ava-labs/gecko/utils/codec" ) func TestCredentialVerify(t *testing.T) { diff --git a/vms/secp256k1fx/fx_test.go b/vms/secp256k1fx/fx_test.go index 79e6c89..566b4cb 100644 --- a/vms/secp256k1fx/fx_test.go +++ b/vms/secp256k1fx/fx_test.go @@ -12,7 +12,7 @@ import ( "github.com/ava-labs/gecko/utils/hashing" "github.com/ava-labs/gecko/utils/logging" "github.com/ava-labs/gecko/utils/timer" - "github.com/ava-labs/gecko/vms/components/codec" + "github.com/ava-labs/gecko/utils/codec" ) var ( diff --git a/vms/secp256k1fx/transer_input_test.go b/vms/secp256k1fx/transer_input_test.go index e954af0..00e894f 100644 --- a/vms/secp256k1fx/transer_input_test.go +++ b/vms/secp256k1fx/transer_input_test.go @@ -7,7 +7,7 @@ import ( "bytes" "testing" - "github.com/ava-labs/gecko/vms/components/codec" + "github.com/ava-labs/gecko/utils/codec" ) func TestTransferInputAmount(t *testing.T) { diff --git a/vms/secp256k1fx/transfer_output_test.go b/vms/secp256k1fx/transfer_output_test.go index 7e87875..09bb0ce 100644 --- a/vms/secp256k1fx/transfer_output_test.go +++ b/vms/secp256k1fx/transfer_output_test.go @@ -8,7 +8,7 @@ import ( "testing" "github.com/ava-labs/gecko/ids" - "github.com/ava-labs/gecko/vms/components/codec" + "github.com/ava-labs/gecko/utils/codec" ) func TestOutputAmount(t *testing.T) { diff --git a/vms/secp256k1fx/vm.go b/vms/secp256k1fx/vm.go index bb59166..37aa23b 100644 --- a/vms/secp256k1fx/vm.go +++ b/vms/secp256k1fx/vm.go @@ -6,7 +6,7 @@ package secp256k1fx import ( "github.com/ava-labs/gecko/utils/logging" "github.com/ava-labs/gecko/utils/timer" - "github.com/ava-labs/gecko/vms/components/codec" + "github.com/ava-labs/gecko/utils/codec" ) // VM that this Fx must be run by diff --git a/vms/timestampvm/vm.go b/vms/timestampvm/vm.go index c571d9a..5376e2f 100644 --- a/vms/timestampvm/vm.go +++ b/vms/timestampvm/vm.go @@ -12,7 +12,7 @@ import ( "github.com/ava-labs/gecko/snow" "github.com/ava-labs/gecko/snow/consensus/snowman" "github.com/ava-labs/gecko/snow/engine/common" - "github.com/ava-labs/gecko/vms/components/codec" + "github.com/ava-labs/gecko/utils/codec" "github.com/ava-labs/gecko/vms/components/core" ) diff --git a/xputtest/avmwallet/wallet.go b/xputtest/avmwallet/wallet.go index ef01eb0..c5d2cd9 100644 --- a/xputtest/avmwallet/wallet.go +++ b/xputtest/avmwallet/wallet.go @@ -19,7 +19,7 @@ import ( "github.com/ava-labs/gecko/utils/wrappers" "github.com/ava-labs/gecko/vms/avm" "github.com/ava-labs/gecko/vms/components/ava" - "github.com/ava-labs/gecko/vms/components/codec" + "github.com/ava-labs/gecko/utils/codec" "github.com/ava-labs/gecko/vms/secp256k1fx" )