Skip to content

Commit

Permalink
ACP-103: Finalize complexity calculations (#3548)
Browse files Browse the repository at this point in the history
  • Loading branch information
StephenButtolph authored Nov 15, 2024
1 parent 72b5c23 commit 5a188b8
Show file tree
Hide file tree
Showing 6 changed files with 188 additions and 204 deletions.
51 changes: 36 additions & 15 deletions utils/crypto/bls/bls_benchmark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,27 @@ import (
"github.com/ava-labs/avalanchego/utils"
)

var sizes = []int{
2,
4,
8,
16,
32,
64,
128,
256,
512,
1024,
2048,
4096,
}
var (
sizes = []int{
1,
2,
4,
8,
16,
32,
64,
128,
256,
512,
1024,
2048,
4096,
8192,
16384,
32768,
}
biggestSize = sizes[len(sizes)-1]
)

func BenchmarkSign(b *testing.B) {
privateKey, err := NewSecretKey()
Expand Down Expand Up @@ -63,7 +70,7 @@ func BenchmarkVerify(b *testing.B) {
}

func BenchmarkAggregatePublicKeys(b *testing.B) {
keys := make([]*PublicKey, 4096)
keys := make([]*PublicKey, biggestSize)
for i := range keys {
privateKey, err := NewSecretKey()
require.NoError(b, err)
Expand Down Expand Up @@ -130,3 +137,17 @@ func BenchmarkPublicKeyFromValidUncompressedBytes(b *testing.B) {
_ = PublicKeyFromValidUncompressedBytes(pkBytes)
}
}

func BenchmarkSignatureFromBytes(b *testing.B) {
privateKey, err := NewSecretKey()
require.NoError(b, err)

message := utils.RandomBytes(32)
signature := Sign(privateKey, message)
signatureBytes := SignatureToBytes(signature)

b.ResetTimer()
for range b.N {
_, _ = SignatureFromBytes(signatureBytes)
}
}
10 changes: 10 additions & 0 deletions vms/platformvm/signer/proof_of_possession_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,16 @@ func TestNewProofOfPossessionDeterministic(t *testing.T) {
require.Equal(blsPOP0, blsPOP1)
}

func BenchmarkProofOfPossessionVerify(b *testing.B) {
pop, err := newProofOfPossession()
require.NoError(b, err)

b.ResetTimer()
for range b.N {
_ = pop.Verify()
}
}

func newProofOfPossession() (*ProofOfPossession, error) {
sk, err := bls.NewSecretKey()
if err != nil {
Expand Down
11 changes: 6 additions & 5 deletions vms/platformvm/txs/executor/standard_tx_executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3642,16 +3642,17 @@ func TestStandardExecutorSetL1ValidatorWeightTx(t *testing.T) {
nil, // chainIDs
)

message := test.message
if message == nil {
message = increaseWeightWarpMessage.Bytes()
}
setL1ValidatorWeightTx, err := wallet.IssueSetL1ValidatorWeightTx(
message,
increaseWeightWarpMessage.Bytes(),
test.builderOptions...,
)
require.NoError(err)

if test.message != nil {
unsignedTx := setL1ValidatorWeightTx.Unsigned.(*txs.SetL1ValidatorWeightTx)
unsignedTx.Message = test.message
}

diff, err := state.NewDiffOn(baseState)
require.NoError(err)

Expand Down
80 changes: 40 additions & 40 deletions vms/platformvm/txs/fee/calculator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"github.com/ava-labs/avalanchego/vms/components/gas"
)

const testDynamicPrice = 100
const testDynamicPrice = gas.Price(units.NanoAvax)

var (
testStaticConfig = StaticConfig{
Expand All @@ -23,9 +23,9 @@ var (
}
testDynamicWeights = gas.Dimensions{
gas.Bandwidth: 1,
gas.DBRead: 200,
gas.DBWrite: 300,
gas.Compute: 0, // TODO: Populate
gas.DBRead: 2000,
gas.DBWrite: 20000,
gas.Compute: 10,
}

// TODO: Rather than hardcoding transactions, consider implementing and
Expand Down Expand Up @@ -76,9 +76,9 @@ var (
gas.Bandwidth: 691, // The length of the tx in bytes
gas.DBRead: IntrinsicAddPermissionlessValidatorTxComplexities[gas.DBRead] + intrinsicInputDBRead,
gas.DBWrite: IntrinsicAddPermissionlessValidatorTxComplexities[gas.DBWrite] + intrinsicInputDBWrite + 2*intrinsicOutputDBWrite,
gas.Compute: 0, // TODO: implement
gas.Compute: intrinsicBLSPoPVerifyCompute + intrinsicSECP256k1FxSignatureCompute,
},
expectedDynamicFee: 229_100,
expectedDynamicFee: 137_191 * units.NanoAvax,
},
{
name: "AddPermissionlessValidatorTx for subnet",
Expand All @@ -88,9 +88,9 @@ var (
gas.Bandwidth: 748, // The length of the tx in bytes
gas.DBRead: IntrinsicAddPermissionlessValidatorTxComplexities[gas.DBRead] + 2*intrinsicInputDBRead,
gas.DBWrite: IntrinsicAddPermissionlessValidatorTxComplexities[gas.DBWrite] + 2*intrinsicInputDBWrite + 3*intrinsicOutputDBWrite,
gas.Compute: 0, // TODO: implement
gas.Compute: 2 * intrinsicSECP256k1FxSignatureCompute,
},
expectedDynamicFee: 314_800,
expectedDynamicFee: 170_748 * units.NanoAvax,
},
{
name: "AddPermissionlessDelegatorTx for primary network",
Expand All @@ -100,9 +100,9 @@ var (
gas.Bandwidth: 499, // The length of the tx in bytes
gas.DBRead: IntrinsicAddPermissionlessDelegatorTxComplexities[gas.DBRead] + 1*intrinsicInputDBRead,
gas.DBWrite: IntrinsicAddPermissionlessDelegatorTxComplexities[gas.DBWrite] + 1*intrinsicInputDBWrite + 2*intrinsicOutputDBWrite,
gas.Compute: 0, // TODO: implement
gas.Compute: intrinsicSECP256k1FxSignatureCompute,
},
expectedDynamicFee: 209_900,
expectedDynamicFee: 106_499 * units.NanoAvax,
},
{
name: "AddPermissionlessDelegatorTx for subnet",
Expand All @@ -112,9 +112,9 @@ var (
gas.Bandwidth: 720, // The length of the tx in bytes
gas.DBRead: IntrinsicAddPermissionlessDelegatorTxComplexities[gas.DBRead] + 2*intrinsicInputDBRead,
gas.DBWrite: IntrinsicAddPermissionlessDelegatorTxComplexities[gas.DBWrite] + 2*intrinsicInputDBWrite + 3*intrinsicOutputDBWrite,
gas.Compute: 0, // TODO: implement
gas.Compute: 2 * intrinsicSECP256k1FxSignatureCompute,
},
expectedDynamicFee: 312_000,
expectedDynamicFee: 150_720 * units.NanoAvax,
},
{
name: "AddSubnetValidatorTx",
Expand All @@ -124,9 +124,9 @@ var (
gas.Bandwidth: 460, // The length of the tx in bytes
gas.DBRead: IntrinsicAddSubnetValidatorTxComplexities[gas.DBRead] + intrinsicInputDBRead,
gas.DBWrite: IntrinsicAddSubnetValidatorTxComplexities[gas.DBWrite] + intrinsicInputDBWrite + intrinsicOutputDBWrite,
gas.Compute: 0, // TODO: implement
gas.Compute: 2 * intrinsicSECP256k1FxSignatureCompute,
},
expectedDynamicFee: 196_000,
expectedDynamicFee: 112_460 * units.NanoAvax,
},
{
name: "BaseTx",
Expand All @@ -136,9 +136,9 @@ var (
gas.Bandwidth: 399, // The length of the tx in bytes
gas.DBRead: IntrinsicBaseTxComplexities[gas.DBRead] + intrinsicInputDBRead,
gas.DBWrite: IntrinsicBaseTxComplexities[gas.DBWrite] + intrinsicInputDBWrite + 2*intrinsicOutputDBWrite,
gas.Compute: 0, // TODO: implement
gas.Compute: intrinsicSECP256k1FxSignatureCompute,
},
expectedDynamicFee: 149_900,
expectedDynamicFee: 64_399 * units.NanoAvax,
},
{
name: "CreateChainTx",
Expand All @@ -148,9 +148,9 @@ var (
gas.Bandwidth: 509, // The length of the tx in bytes
gas.DBRead: IntrinsicCreateChainTxComplexities[gas.DBRead] + intrinsicInputDBRead,
gas.DBWrite: IntrinsicCreateChainTxComplexities[gas.DBWrite] + intrinsicInputDBWrite + intrinsicOutputDBWrite,
gas.Compute: 0, // TODO: implement
gas.Compute: 2 * intrinsicSECP256k1FxSignatureCompute,
},
expectedDynamicFee: 180_900,
expectedDynamicFee: 72_509 * units.NanoAvax,
},
{
name: "CreateSubnetTx",
Expand All @@ -160,9 +160,9 @@ var (
gas.Bandwidth: 339, // The length of the tx in bytes
gas.DBRead: IntrinsicCreateSubnetTxComplexities[gas.DBRead] + intrinsicInputDBRead,
gas.DBWrite: IntrinsicCreateSubnetTxComplexities[gas.DBWrite] + intrinsicInputDBWrite + intrinsicOutputDBWrite,
gas.Compute: 0, // TODO: implement
gas.Compute: intrinsicSECP256k1FxSignatureCompute,
},
expectedDynamicFee: 143_900,
expectedDynamicFee: 64_339 * units.NanoAvax,
},
{
name: "ExportTx",
Expand All @@ -172,9 +172,9 @@ var (
gas.Bandwidth: 435, // The length of the tx in bytes
gas.DBRead: IntrinsicExportTxComplexities[gas.DBRead] + intrinsicInputDBRead,
gas.DBWrite: IntrinsicExportTxComplexities[gas.DBWrite] + intrinsicInputDBWrite + 2*intrinsicOutputDBWrite,
gas.Compute: 0, // TODO: implement
gas.Compute: intrinsicSECP256k1FxSignatureCompute,
},
expectedDynamicFee: 153_500,
expectedDynamicFee: 64_435 * units.NanoAvax,
},
{
name: "ImportTx",
Expand All @@ -184,9 +184,9 @@ var (
gas.Bandwidth: 335, // The length of the tx in bytes
gas.DBRead: IntrinsicImportTxComplexities[gas.DBRead] + intrinsicInputDBRead,
gas.DBWrite: IntrinsicImportTxComplexities[gas.DBWrite] + intrinsicInputDBWrite + intrinsicOutputDBWrite,
gas.Compute: 0, // TODO: implement
gas.Compute: intrinsicSECP256k1FxSignatureCompute,
},
expectedDynamicFee: 113_500,
expectedDynamicFee: 44_335 * units.NanoAvax,
},
{
name: "RemoveSubnetValidatorTx",
Expand All @@ -196,9 +196,9 @@ var (
gas.Bandwidth: 436, // The length of the tx in bytes
gas.DBRead: IntrinsicRemoveSubnetValidatorTxComplexities[gas.DBRead] + intrinsicInputDBRead,
gas.DBWrite: IntrinsicRemoveSubnetValidatorTxComplexities[gas.DBWrite] + intrinsicInputDBWrite + intrinsicOutputDBWrite,
gas.Compute: 0, // TODO: implement
gas.Compute: 2 * intrinsicSECP256k1FxSignatureCompute,
},
expectedDynamicFee: 193_600,
expectedDynamicFee: 108_436 * units.NanoAvax,
},
{
name: "TransformSubnetTx",
Expand All @@ -215,9 +215,9 @@ var (
gas.Bandwidth: 436, // The length of the tx in bytes
gas.DBRead: IntrinsicTransferSubnetOwnershipTxComplexities[gas.DBRead] + intrinsicInputDBRead,
gas.DBWrite: IntrinsicTransferSubnetOwnershipTxComplexities[gas.DBWrite] + intrinsicInputDBWrite + intrinsicOutputDBWrite,
gas.Compute: 0, // TODO: implement
gas.Compute: 2 * intrinsicSECP256k1FxSignatureCompute,
},
expectedDynamicFee: 173_600,
expectedDynamicFee: 68_436 * units.NanoAvax,
},
{
name: "ConvertSubnetToL1Tx",
Expand All @@ -227,33 +227,33 @@ var (
gas.Bandwidth: 656, // The length of the tx in bytes
gas.DBRead: IntrinsicConvertSubnetToL1TxComplexities[gas.DBRead] + intrinsicInputDBRead,
gas.DBWrite: IntrinsicConvertSubnetToL1TxComplexities[gas.DBWrite] + intrinsicInputDBWrite + intrinsicOutputDBWrite + intrinsicConvertSubnetToL1ValidatorDBWrite,
gas.Compute: 0, // TODO: implement
gas.Compute: 2*intrinsicSECP256k1FxSignatureCompute + intrinsicBLSPoPVerifyCompute,
},
expectedDynamicFee: 365_600,
expectedDynamicFee: 183_156 * units.NanoAvax,
},
{
name: "RegisterL1ValidatorTx",
tx: "00000000002400003039000000000000000000000000000000000000000000000000000000000000000000000001dbcf890f77f49b96857648b72b77f9f82937f28a68704af05da0dc12ba53f2db00000007002386f1f88b552a000000000000000000000001000000013cb7d3842e8cee6a0ebd09f1fe884f6861e1b29c00000001ca44ad45a63381b07074be7f82005c41550c989b967f40020f3bedc4b02191f300000000dbcf890f77f49b96857648b72b77f9f82937f28a68704af05da0dc12ba53f2db00000005002386f234262404000000010000000000000000000000003b9aca00ab5cb0516b7afdb13727f766185b2b8da44e2653eef63c85f196701083e649289cce1a23c39eb471b2473bc6872aa3ea190de0fe66296cbdd4132c92c3430ff22f28f0b341b15905a005bbd66cc0f4056bc4be5934e4f3a57151a60060f429190000012f000000003039705f3d4415f990225d3df5ce437d7af2aa324b1bbce854ee34ab6f39882250d20000009c000000000001000000000000008e000000000001a0673b4ee5ec44e57c8ab250dd7cd7b68d04421f64bd6559a4284a3ee358ff2b000000145efc86a11c5b12cc95b2cf527c023f9cf6e0e8f6b62034315c5d11cea4190f6ea8997821c02483d29adb5e4567843f7a44c39b2ffa20c8520dc358702fb1ec29f2746dcc000000006705af280000000000000000000000000000000000000000000000010000000000000001018e99dc6ed736089c03b9a1275e0cf801524ed341fb10111f29c0390fa2f96cf6aa78539ec767e5cd523c606c7ede50e60ba6065a3685e770d979b0df74e3541b61ed63f037463776098576e385767a695de59352b44e515831c5ee7a8cc728f9000000010000000900000001a0950b9e6e866130f0d09e2a7bfdd0246513295237258afa942b1850dab79824605c796bbfc9223cf91935fb29c66f8b927690220b9b1c24d6f078054a3e346201",
expectedStaticFeeErr: ErrUnsupportedTx,
expectedComplexity: gas.Dimensions{
gas.Bandwidth: 710, // The length of the tx in bytes
gas.DBRead: IntrinsicRegisterL1ValidatorTxComplexities[gas.DBRead] + intrinsicInputDBRead,
gas.DBRead: IntrinsicRegisterL1ValidatorTxComplexities[gas.DBRead] + intrinsicInputDBRead + intrinsicWarpDBReads,
gas.DBWrite: IntrinsicRegisterL1ValidatorTxComplexities[gas.DBWrite] + intrinsicInputDBWrite + intrinsicOutputDBWrite,
gas.Compute: 0, // TODO: implement
gas.Compute: intrinsicSECP256k1FxSignatureCompute + intrinsicBLSPoPVerifyCompute + intrinsicBLSAggregateCompute + intrinsicBLSVerifyCompute,
},
expectedDynamicFee: 151_000,
expectedDynamicFee: 241_260 * units.NanoAvax,
},
{
name: "SetL1ValidatorWeightTx",
tx: "00000000002500003039000000000000000000000000000000000000000000000000000000000000000000000001dbcf890f77f49b96857648b72b77f9f82937f28a68704af05da0dc12ba53f2db00000007002386f1f88b5100000000000000000000000001000000013cb7d3842e8cee6a0ebd09f1fe884f6861e1b29c00000001389c41b6ed301e4c118bd23673268fd2054b772efcf25685a117b74bab7ae5e400000000dbcf890f77f49b96857648b72b77f9f82937f28a68704af05da0dc12ba53f2db00000005002386f1f88b552a000000010000000000000000000000d7000000003039705f3d4415f990225d3df5ce437d7af2aa324b1bbce854ee34ab6f39882250d200000044000000000001000000000000003600000000000338e6e9fe31c6d070a8c792dbacf6d0aefb8eac2aded49cc0aa9f422d1fdd9ecd0000000000000001000000000000000500000000000000010187f4bb2c42869c56f023a1ca81045aff034acd490b8f15b5069025f982e605e077007fc588f7d56369a65df7574df3b70ff028ea173739c789525ab7eebfcb5c115b13cca8f02b362104b700c75bc95234109f3f1360ddcb4ec3caf6b0e821cb0000000100000009000000010a29f3c86d52908bf2efbc3f918a363df704c429d66c8d6615712a2a584a2a5f264a9e7b107c07122a06f31cadc2f51285884d36fe8df909a07467417f1d64cf00",
expectedStaticFeeErr: ErrUnsupportedTx,
expectedComplexity: gas.Dimensions{
gas.Bandwidth: 518, // The length of the tx in bytes
gas.DBRead: IntrinsicSetL1ValidatorWeightTxComplexities[gas.DBRead] + intrinsicInputDBRead,
gas.DBRead: IntrinsicSetL1ValidatorWeightTxComplexities[gas.DBRead] + intrinsicInputDBRead + intrinsicWarpDBReads,
gas.DBWrite: IntrinsicSetL1ValidatorWeightTxComplexities[gas.DBWrite] + intrinsicInputDBWrite + intrinsicOutputDBWrite,
gas.Compute: 0, // TODO: implement
gas.Compute: intrinsicSECP256k1FxSignatureCompute + intrinsicBLSAggregateCompute + intrinsicBLSVerifyCompute,
},
expectedDynamicFee: 131_800,
expectedDynamicFee: 206_568 * units.NanoAvax,
},
{
name: "IncreaseL1ValidatorBalanceTx",
Expand All @@ -263,9 +263,9 @@ var (
gas.Bandwidth: 339, // The length of the tx in bytes
gas.DBRead: IntrinsicIncreaseL1ValidatorBalanceTxComplexities[gas.DBRead] + intrinsicInputDBRead,
gas.DBWrite: IntrinsicIncreaseL1ValidatorBalanceTxComplexities[gas.DBWrite] + intrinsicInputDBWrite + intrinsicOutputDBWrite,
gas.Compute: 0, // TODO: implement
gas.Compute: intrinsicSECP256k1FxSignatureCompute,
},
expectedDynamicFee: 113_900,
expectedDynamicFee: 146_339 * units.NanoAvax,
},
{
name: "DisableL1ValidatorTx",
Expand All @@ -275,9 +275,9 @@ var (
gas.Bandwidth: 347, // The length of the tx in bytes
gas.DBRead: IntrinsicDisableL1ValidatorTxComplexities[gas.DBRead] + intrinsicInputDBRead,
gas.DBWrite: IntrinsicDisableL1ValidatorTxComplexities[gas.DBWrite] + intrinsicInputDBWrite + intrinsicOutputDBWrite,
gas.Compute: 0, // TODO: implement
gas.Compute: intrinsicSECP256k1FxSignatureCompute,
},
expectedDynamicFee: 114_700,
expectedDynamicFee: 166_347 * units.NanoAvax,
},
}
)
Loading

0 comments on commit 5a188b8

Please sign in to comment.