Skip to content

Commit

Permalink
Squashed commit of the following:
Browse files Browse the repository at this point in the history
commit 82fbc97
Author: Stephen Buttolph <[email protected]>
Date:   Tue Dec 12 18:30:09 2023 -0500

    Add ACP signaling (#2476)

commit ac5a00e
Author: Joshua Kim <[email protected]>
Date:   Tue Dec 12 17:42:32 2023 -0500

    Refactor p2p unit tests (#2475)

    Signed-off-by: Joshua Kim <[email protected]>
    Co-authored-by: Dan Laine <[email protected]>

commit 0b2b109
Author: Dhruba Basu <[email protected]>
Date:   Tue Dec 12 16:48:28 2023 -0500

    `vms/platformvm`: Verify txs before building a block (#2359)

    Co-authored-by: Stephen Buttolph <[email protected]>

commit 4be744e
Author: Joshua Kim <[email protected]>
Date:   Tue Dec 12 15:08:48 2023 -0500

    P2P AppError handling (#2248)

    Signed-off-by: Joshua Kim <[email protected]>
    Co-authored-by: Stephen Buttolph <[email protected]>

commit 7963115
Author: Dhruba Basu <[email protected]>
Date:   Tue Dec 12 14:37:59 2023 -0500

    `vms/platformvm`: Add `TestBuildBlockForceAdvanceTime` test (#2472)

    Co-authored-by: Stephen Buttolph <[email protected]>

commit dc472ec
Author: Dhruba Basu <[email protected]>
Date:   Tue Dec 12 14:37:43 2023 -0500

    `vms/platformvm`: Permit usage of the `Transactions` field in `BanffProposalBlock` (#2451)

    Co-authored-by: Stephen Buttolph <[email protected]>

Signed-off-by: Joshua Kim <[email protected]>
  • Loading branch information
joshua-kim committed Dec 13, 2023
1 parent 5c0f12e commit f5f9177
Show file tree
Hide file tree
Showing 55 changed files with 2,019 additions and 1,464 deletions.
63 changes: 63 additions & 0 deletions api/info/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@ import (
"github.com/ava-labs/avalanchego/network"
"github.com/ava-labs/avalanchego/network/peer"
"github.com/ava-labs/avalanchego/snow/networking/benchlist"
"github.com/ava-labs/avalanchego/snow/validators"
"github.com/ava-labs/avalanchego/utils/constants"
"github.com/ava-labs/avalanchego/utils/ips"
"github.com/ava-labs/avalanchego/utils/json"
"github.com/ava-labs/avalanchego/utils/logging"
"github.com/ava-labs/avalanchego/utils/set"
"github.com/ava-labs/avalanchego/version"
"github.com/ava-labs/avalanchego/vms"
"github.com/ava-labs/avalanchego/vms/platformvm/signer"
Expand All @@ -32,6 +34,7 @@ var errNoChainProvided = errors.New("argument 'chain' not given")
type Info struct {
Parameters
log logging.Logger
validators validators.Manager
myIP ips.DynamicIPPort
networking network.Network
chainManager chains.Manager
Expand Down Expand Up @@ -59,6 +62,7 @@ type Parameters struct {
func NewService(
parameters Parameters,
log logging.Logger,
validators validators.Manager,
chainManager chains.Manager,
vmManager vms.Manager,
myIP ips.DynamicIPPort,
Expand All @@ -73,6 +77,7 @@ func NewService(
&Info{
Parameters: parameters,
log: log,
validators: validators,
chainManager: chainManager,
vmManager: vmManager,
myIP: myIP,
Expand Down Expand Up @@ -319,6 +324,64 @@ func (i *Info) Uptime(_ *http.Request, args *UptimeRequest, reply *UptimeRespons
return nil
}

type ACP struct {
SupportWeight json.Uint64 `json:"supportWeight"`
Supporters set.Set[ids.NodeID] `json:"supporters"`
ObjectWeight json.Uint64 `json:"objectWeight"`
Objectors set.Set[ids.NodeID] `json:"objectors"`
AbstainWeight json.Uint64 `json:"abstainWeight"`
}

type ACPsReply struct {
ACPs map[uint32]*ACP `json:"acps"`
}

func (a *ACPsReply) getACP(acpNum uint32) *ACP {
acp, ok := a.ACPs[acpNum]
if !ok {
acp = &ACP{}
a.ACPs[acpNum] = acp
}
return acp
}

func (i *Info) Acps(_ *http.Request, _ *struct{}, reply *ACPsReply) error {
i.log.Debug("API called",
zap.String("service", "info"),
zap.String("method", "acps"),
)

reply.ACPs = make(map[uint32]*ACP, constants.CurrentACPs.Len())
peers := i.networking.PeerInfo(nil)
for _, peer := range peers {
weight := json.Uint64(i.validators.GetWeight(constants.PrimaryNetworkID, peer.ID))
if weight == 0 {
continue
}

for acpNum := range peer.SupportedACPs {
acp := reply.getACP(acpNum)
acp.Supporters.Add(peer.ID)
acp.SupportWeight += weight
}
for acpNum := range peer.ObjectedACPs {
acp := reply.getACP(acpNum)
acp.Objectors.Add(peer.ID)
acp.ObjectWeight += weight
}
}

totalWeight, err := i.validators.TotalWeight(constants.PrimaryNetworkID)
if err != nil {
return err
}
for acpNum := range constants.CurrentACPs {
acp := reply.getACP(acpNum)
acp.AbstainWeight = json.Uint64(totalWeight) - acp.SupportWeight - acp.ObjectWeight
}
return nil
}

type GetTxFeeResponse struct {
TxFee json.Uint64 `json:"txFee"`
CreateAssetTxFee json.Uint64 `json:"createAssetTxFee"`
Expand Down
23 changes: 23 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ var (
ConsensusGossipOnAcceptPeerSizeKey: acceptedFrontierGossipDeprecationMsg,
}

errConflictingACPOpinion = errors.New("supporting and objecting to the same ACP")
errSybilProtectionDisabledStakerWeights = errors.New("sybil protection disabled weights must be positive")
errSybilProtectionDisabledOnPublicNetwork = errors.New("sybil protection disabled on public network")
errAuthPasswordTooWeak = errors.New("API auth password is not strong enough")
Expand Down Expand Up @@ -346,6 +347,25 @@ func getNetworkConfig(
allowPrivateIPs = v.GetBool(NetworkAllowPrivateIPsKey)
}

var supportedACPs set.Set[uint32]
for _, acp := range v.GetIntSlice(ACPSupportKey) {
if acp < 0 || acp > math.MaxInt32 {
return network.Config{}, fmt.Errorf("invalid ACP: %d", acp)
}
supportedACPs.Add(uint32(acp))
}

var objectedACPs set.Set[uint32]
for _, acp := range v.GetIntSlice(ACPObjectKey) {
if acp < 0 || acp > math.MaxInt32 {
return network.Config{}, fmt.Errorf("invalid ACP: %d", acp)
}
objectedACPs.Add(uint32(acp))
}
if supportedACPs.Overlaps(objectedACPs) {
return network.Config{}, errConflictingACPOpinion
}

config := network.Config{
ThrottlerConfig: network.ThrottlerConfig{
MaxInboundConnsPerSec: maxInboundConnsPerSec,
Expand Down Expand Up @@ -425,6 +445,9 @@ func getNetworkConfig(
UptimeMetricFreq: v.GetDuration(UptimeMetricFreqKey),
MaximumInboundMessageTimeout: v.GetDuration(NetworkMaximumInboundTimeoutKey),

SupportedACPs: supportedACPs,
ObjectedACPs: objectedACPs,

RequireValidatorToConnect: v.GetBool(NetworkRequireValidatorToConnectKey),
PeerReadBufferSize: int(v.GetUint(NetworkPeerReadBufferSizeKey)),
PeerWriteBufferSize: int(v.GetUint(NetworkPeerWriteBufferSizeKey)),
Expand Down
4 changes: 4 additions & 0 deletions config/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,10 @@ func addNodeFlags(fs *pflag.FlagSet) {
// Network ID
fs.String(NetworkNameKey, constants.MainnetName, "Network ID this node will connect to")

// ACP flagging
fs.IntSlice(ACPSupportKey, nil, "ACPs to support adoption")
fs.IntSlice(ACPObjectKey, nil, "ACPs to object adoption")

// AVAX fees
fs.Uint64(TxFeeKey, genesis.LocalParams.TxFee, "Transaction fee, in nAVAX")
fs.Uint64(CreateAssetTxFeeKey, genesis.LocalParams.CreateAssetTxFee, "Transaction fee, in nAVAX, for transactions that create new assets")
Expand Down
2 changes: 2 additions & 0 deletions config/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ const (
GenesisFileKey = "genesis-file"
GenesisFileContentKey = "genesis-file-content"
NetworkNameKey = "network-id"
ACPSupportKey = "acp-support"
ACPObjectKey = "acp-object"
TxFeeKey = "tx-fee"
CreateAssetTxFeeKey = "create-asset-tx-fee"
CreateSubnetTxFeeKey = "create-subnet-tx-fee"
Expand Down
20 changes: 20 additions & 0 deletions message/inbound_msg_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,26 @@ func InboundAppRequest(
}
}

func InboundAppError(
nodeID ids.NodeID,
chainID ids.ID,
requestID uint32,
errorCode int32,
errorMessage string,
) InboundMessage {
return &inboundMessage{
nodeID: nodeID,
op: AppErrorOp,
message: &p2p.AppError{
ChainId: chainID[:],
RequestId: requestID,
ErrorCode: errorCode,
ErrorMessage: errorMessage,
},
expiration: mockable.MaxTime,
}
}

func InboundAppResponse(
chainID ids.ID,
requestID uint32,
Expand Down
44 changes: 44 additions & 0 deletions message/inbound_msg_builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (

"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/proto/pb/p2p"
"github.com/ava-labs/avalanchego/utils/compression"
"github.com/ava-labs/avalanchego/utils/logging"
"github.com/ava-labs/avalanchego/utils/timer/mockable"
)
Expand Down Expand Up @@ -396,3 +397,46 @@ func TestInboundMsgBuilder(t *testing.T) {
},
)
}

func TestAppError(t *testing.T) {
require := require.New(t)

mb, err := newMsgBuilder(
logging.NoLog{},
"",
prometheus.NewRegistry(),
time.Second,
)
require.NoError(err)

nodeID := ids.GenerateTestNodeID()
chainID := ids.GenerateTestID()
requestID := uint32(1)
errorCode := int32(2)
errorMessage := "hello world"

want := &p2p.Message{
Message: &p2p.Message_AppError{
AppError: &p2p.AppError{
ChainId: chainID[:],
RequestId: requestID,
ErrorCode: errorCode,
ErrorMessage: errorMessage,
},
},
}

outMsg, err := mb.createOutbound(want, compression.TypeNone, false)
require.NoError(err)

got, err := mb.parseInbound(outMsg.Bytes(), nodeID, func() {})
require.NoError(err)

require.Equal(nodeID, got.NodeID())
require.Equal(AppErrorOp, got.Op())

msg, ok := got.Message().(*p2p.AppError)
require.True(ok)
require.Equal(errorCode, msg.ErrorCode)
require.Equal(errorMessage, msg.ErrorMessage)
}
50 changes: 8 additions & 42 deletions message/internal_msg_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,6 @@ var (
_ requestIDGetter = (*QueryFailed)(nil)
_ engineTypeGetter = (*QueryFailed)(nil)

_ fmt.Stringer = (*AppRequestFailed)(nil)
_ chainIDGetter = (*AppRequestFailed)(nil)
_ requestIDGetter = (*AppRequestFailed)(nil)

_ fmt.Stringer = (*CrossChainAppRequest)(nil)
_ sourceChainIDGetter = (*CrossChainAppRequest)(nil)
_ chainIDGetter = (*CrossChainAppRequest)(nil)
Expand Down Expand Up @@ -365,42 +361,6 @@ func InternalQueryFailed(
}
}

type AppRequestFailed struct {
ChainID ids.ID `json:"chain_id,omitempty"`
RequestID uint32 `json:"request_id,omitempty"`
}

func (m *AppRequestFailed) String() string {
return fmt.Sprintf(
"ChainID: %s RequestID: %d",
m.ChainID, m.RequestID,
)
}

func (m *AppRequestFailed) GetChainId() []byte {
return m.ChainID[:]
}

func (m *AppRequestFailed) GetRequestId() uint32 {
return m.RequestID
}

func InternalAppRequestFailed(
nodeID ids.NodeID,
chainID ids.ID,
requestID uint32,
) InboundMessage {
return &inboundMessage{
nodeID: nodeID,
op: AppRequestFailedOp,
message: &AppRequestFailed{
ChainID: chainID,
RequestID: requestID,
},
expiration: mockable.MaxTime,
}
}

type CrossChainAppRequest struct {
SourceChainID ids.ID `json:"source_chain_id,omitempty"`
DestinationChainID ids.ID `json:"destination_chain_id,omitempty"`
Expand Down Expand Up @@ -452,6 +412,8 @@ type CrossChainAppRequestFailed struct {
SourceChainID ids.ID `json:"source_chain_id,omitempty"`
DestinationChainID ids.ID `json:"destination_chain_id,omitempty"`
RequestID uint32 `json:"request_id,omitempty"`
ErrorCode int32 `json:"error_code,omitempty"`
ErrorMessage string `json:"error_message,omitempty"`
}

func (m *CrossChainAppRequestFailed) String() string {
Expand All @@ -473,19 +435,23 @@ func (m *CrossChainAppRequestFailed) GetRequestId() uint32 {
return m.RequestID
}

func InternalCrossChainAppRequestFailed(
func InternalCrossChainAppError(
nodeID ids.NodeID,
sourceChainID ids.ID,
destinationChainID ids.ID,
requestID uint32,
errorCode int32,
errorMessage string,
) InboundMessage {
return &inboundMessage{
nodeID: nodeID,
op: CrossChainAppRequestFailedOp,
op: CrossChainAppErrorOp,
message: &CrossChainAppRequestFailed{
SourceChainID: sourceChainID,
DestinationChainID: destinationChainID,
RequestID: requestID,
ErrorCode: errorCode,
ErrorMessage: errorMessage,
},
expiration: mockable.MaxTime,
}
Expand Down
8 changes: 4 additions & 4 deletions message/mock_outbound_message_builder.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit f5f9177

Please sign in to comment.