Skip to content

Commit

Permalink
SDK refactor from tooling sdk repo (#2137)
Browse files Browse the repository at this point in the history
* sdk refactor

* udpate avalanche sdk

* update pkg

* update ci

* Subnet sdk refactor (#2138)

* refactor subnet sdk create

* update pkg

* fix lint

* remove unused sdk

* fix lint

* fix test

* fix test

* fix test

* rename network

* rename to blockchain

* refactor sdk path

* update sdk

* update sdk

* update sdk

* update sdk

* update sdk

* address comments

* fix lint

* fix lint

* fix lint

---------

Signed-off-by: sukantoraymond <[email protected]>
  • Loading branch information
sukantoraymond authored Sep 12, 2024
1 parent 8bfe436 commit 900c578
Show file tree
Hide file tree
Showing 24 changed files with 2,342 additions and 90 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/lint-and-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ jobs:
go-version: ${{ matrix.go }}
- run: go mod download
- run: scripts/build.sh
- run: go test -v -coverprofile=coverage.out $(go list ./... | grep -v /tests/)
- run: go test -v -coverprofile=coverage.out $(go list ./... | grep -v /tests/ | grep -v '/sdk/')
env:
CGO_CFLAGS: "-O -D__BLST_PORTABLE__" # Set the CGO flags to use the portable version of BLST
- run: go tool cover -func=coverage.out
Expand Down
20 changes: 8 additions & 12 deletions cmd/blockchaincmd/upgradecmd/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/ava-labs/avalanche-cli/pkg/utils"
"github.com/ava-labs/avalanche-cli/pkg/ux"
"github.com/ava-labs/avalanche-cli/pkg/vm"
avalancheSDK "github.com/ava-labs/avalanche-cli/sdk/vm"
"github.com/ava-labs/avalanchego/utils/logging"
"github.com/ava-labs/avalanchego/utils/units"
"github.com/ava-labs/coreth/ethclient"
Expand All @@ -33,14 +34,9 @@ import (
)

const (
blockTimestampKey = "blockTimestamp"
feeConfigKey = "initialFeeConfig"
initialMintKey = "initialMint"
adminAddressesKey = "adminAddresses"
managerAddressesKey = "managerAddresses"
enabledAddressesKey = "enabledAddresses"

enabledLabel = "enabled"
feeConfigKey = "initialFeeConfig"
initialMintKey = "initialMint"

managerLabel = "manager"
adminLabel = "admin"

Expand Down Expand Up @@ -393,7 +389,7 @@ func GetFeeConfig(config params.ChainConfig, useDefault bool) (
setGasStep = "Set block gas cost step"
)

config.FeeConfig = vm.StarterFeeConfig
config.FeeConfig = avalancheSDK.StarterFeeConfig

if useDefault {
config.FeeConfig.GasLimit = vm.LowGasLimit
Expand Down Expand Up @@ -421,13 +417,13 @@ func GetFeeConfig(config params.ChainConfig, useDefault bool) (

switch feeDefault {
case lowOption:
vm.SetStandardGas(&config, vm.LowGasLimit, vm.LowTargetGas, useDynamicFees)
vm.SetStandardGas(&config.FeeConfig, vm.LowGasLimit, vm.LowTargetGas, useDynamicFees)
return config, nil
case mediumOption:
vm.SetStandardGas(&config, vm.MediumGasLimit, vm.MediumTargetGas, useDynamicFees)
vm.SetStandardGas(&config.FeeConfig, vm.MediumGasLimit, vm.MediumTargetGas, useDynamicFees)
return config, err
case highOption:
vm.SetStandardGas(&config, vm.HighGasLimit, vm.HighTargetGas, useDynamicFees)
vm.SetStandardGas(&config.FeeConfig, vm.HighGasLimit, vm.HighTargetGas, useDynamicFees)
return config, err
default:
ux.Logger.PrintToUser("Customizing fee config")
Expand Down
36 changes: 17 additions & 19 deletions pkg/vm/create_evm.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ import (
"github.com/ava-labs/avalanche-cli/pkg/models"
"github.com/ava-labs/avalanche-cli/pkg/teleporter"
"github.com/ava-labs/avalanche-cli/pkg/ux"
blockchainSDK "github.com/ava-labs/avalanche-cli/sdk/blockchain"
"github.com/ava-labs/subnet-evm/core"
subnetevmparams "github.com/ava-labs/subnet-evm/params"
"github.com/ava-labs/subnet-evm/utils"
"github.com/ethereum/go-ethereum/common"
)
Expand Down Expand Up @@ -78,15 +78,7 @@ func CreateEVMGenesis(
) ([]byte, error) {
ux.Logger.PrintToUser("creating genesis for blockchain %s", blockchainName)

genesis := core.Genesis{}
genesis.Timestamp = *utils.TimeToNewUint64(time.Now())
conf := subnetevmparams.SubnetEVMDefaultChainConfig
conf.NetworkUpgrades = subnetevmparams.NetworkUpgrades{}

chainID := new(big.Int).SetUint64(params.chainID)
conf.ChainID = chainID

setFeeConfig(params, conf)
feeConfig := getFeeConfig(params)

// Validity checks on the parameter settings.
if params.enableTransactionPrecompile {
Expand Down Expand Up @@ -121,29 +113,35 @@ func CreateEVMGenesis(
)
}

getPrecompiles(conf, params, &genesis.Timestamp)
genesisBlock0Timestamp := utils.TimeToNewUint64(time.Now())
precompiles := getPrecompiles(params, genesisBlock0Timestamp)

if params.UseTeleporter || params.UseExternalGasToken {
addTeleporterAddressesToAllowLists(
conf,
&precompiles,
teleporterInfo.FundedAddress,
teleporterInfo.MessengerDeployerAddress,
teleporterInfo.RelayerAddress,
)
}

genesis.Alloc = params.initialTokenAllocation
genesis.Config = conf
genesis.Difficulty = Difficulty
genesis.GasLimit = conf.FeeConfig.GasLimit.Uint64()

jsonBytes, err := genesis.MarshalJSON()
subnetConfig, err := blockchainSDK.New(
&blockchainSDK.SubnetParams{
SubnetEVM: &blockchainSDK.SubnetEVMParams{
ChainID: new(big.Int).SetUint64(params.chainID),
FeeConfig: feeConfig,
Allocation: params.initialTokenAllocation,
Precompiles: precompiles,
Timestamp: genesisBlock0Timestamp,
},
Name: "TestSubnet",
})
if err != nil {
return nil, err
}

var prettyJSON bytes.Buffer
err = json.Indent(&prettyJSON, jsonBytes, "", " ")
err = json.Indent(&prettyJSON, subnetConfig.Genesis, "", " ")
if err != nil {
return nil, err
}
Expand Down
15 changes: 0 additions & 15 deletions pkg/vm/evm_settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,10 @@ package vm
import (
"math/big"

"github.com/ava-labs/subnet-evm/commontype"
"github.com/ethereum/go-ethereum/common"
)

var (
Difficulty = big.NewInt(0)

// current avacloud settings
LowGasLimit = big.NewInt(12_000_000)
MediumGasLimit = big.NewInt(15_000_000) // C-Chain value
Expand All @@ -23,18 +20,6 @@ var (

NoDynamicFeesGasLimitToTargetGasFactor = big.NewInt(5)

// This is the current c-chain gas config
StarterFeeConfig = commontype.FeeConfig{
GasLimit: big.NewInt(15_000_000),
MinBaseFee: big.NewInt(25_000_000_000),
TargetGas: big.NewInt(15_000_000),
BaseFeeChangeDenominator: big.NewInt(36),
MinBlockGasCost: big.NewInt(0),
MaxBlockGasCost: big.NewInt(1_000_000),
TargetBlockRate: 2,
BlockGasCostStep: big.NewInt(200_000),
}

PrefundedEwoqAddress = common.HexToAddress("0x8db97C7cEcE249c2b98bDC0226Cc4C2A57BF52FC")
PrefundedEwoqPrivate = "56289e99c94b6912bfc12adc093c9b51124f0dc54ac7a766b2bc5ccf558d8027"

Expand Down
34 changes: 16 additions & 18 deletions pkg/vm/fees.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,46 +6,44 @@ package vm
import (
"math/big"

"github.com/ava-labs/avalanche-cli/sdk/vm"
"github.com/ava-labs/subnet-evm/commontype"
"github.com/ava-labs/subnet-evm/params"
)

func SetStandardGas(
config *params.ChainConfig,
feeConfig *commontype.FeeConfig,
gasLimit *big.Int,
targetGas *big.Int,
useDynamicFees bool,
) {
config.FeeConfig.GasLimit = gasLimit
config.FeeConfig.TargetGas = targetGas
feeConfig.GasLimit = gasLimit
feeConfig.TargetGas = targetGas
if !useDynamicFees {
config.FeeConfig.TargetGas = config.FeeConfig.TargetGas.Mul(config.FeeConfig.GasLimit, NoDynamicFeesGasLimitToTargetGasFactor)
feeConfig.TargetGas = feeConfig.TargetGas.Mul(feeConfig.GasLimit, NoDynamicFeesGasLimitToTargetGasFactor)
}
}

func setFeeConfig(
func getFeeConfig(
params SubnetEVMGenesisParams,
config *params.ChainConfig,
) {
config.FeeConfig = StarterFeeConfig

) commontype.FeeConfig {
feeConfig := vm.StarterFeeConfig
switch {
case params.feeConfig.lowThroughput:
SetStandardGas(config, LowGasLimit, LowTargetGas, params.feeConfig.useDynamicFees)
SetStandardGas(&feeConfig, LowGasLimit, LowTargetGas, params.feeConfig.useDynamicFees)
case params.feeConfig.mediumThroughput:
SetStandardGas(config, MediumGasLimit, MediumTargetGas, params.feeConfig.useDynamicFees)
SetStandardGas(&feeConfig, MediumGasLimit, MediumTargetGas, params.feeConfig.useDynamicFees)
case params.feeConfig.highThroughput:
SetStandardGas(config, HighGasLimit, HighTargetGas, params.feeConfig.useDynamicFees)
SetStandardGas(&feeConfig, HighGasLimit, HighTargetGas, params.feeConfig.useDynamicFees)
default:
setCustomFeeConfig(params, config)
feeConfig = getCustomFeeConfig(params)
}
return feeConfig
}

func setCustomFeeConfig(
func getCustomFeeConfig(
params SubnetEVMGenesisParams,
config *params.ChainConfig,
) {
config.FeeConfig = commontype.FeeConfig{
) commontype.FeeConfig {
return commontype.FeeConfig{
GasLimit: params.feeConfig.gasLimit,
TargetBlockRate: params.feeConfig.blockRate.Uint64(),
MinBaseFee: params.feeConfig.minBaseFee,
Expand Down
50 changes: 25 additions & 25 deletions pkg/vm/precompiles.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,8 @@ func configureWarp(timestamp *uint64) warp.Config {
}
}

// adds teleporter-related addresses (main funded key, messenger deploy key, relayer key)
// to the allow list of relevant enabled precompiles
func addTeleporterAddressesToAllowLists(
config *params.ChainConfig,
precompile *params.Precompiles,
teleporterAddress string,
teleporterMessengerDeployerAddress string,
relayerAddress string,
Expand All @@ -118,7 +116,8 @@ func addTeleporterAddressesToAllowLists(
// teleporterAddress funds the other two and also deploys the registry
// teleporterMessengerDeployerAddress deploys the messenger
// relayerAddress is used by the relayer to send txs to the target chain
precompileConfig := config.GenesisPrecompiles[txallowlist.ConfigKey]
currentPrecompile := *precompile
precompileConfig := currentPrecompile[txallowlist.ConfigKey]
if precompileConfig != nil {
txAllowListConfig := precompileConfig.(*txallowlist.Config)
for _, address := range []string{teleporterAddress, teleporterMessengerDeployerAddress, relayerAddress} {
Expand All @@ -131,7 +130,7 @@ func addTeleporterAddressesToAllowLists(
// contract deploy allow list:
// teleporterAddress deploys the registry
// teleporterMessengerDeployerAddress deploys the messenger
precompileConfig = config.GenesisPrecompiles[deployerallowlist.ConfigKey]
precompileConfig = currentPrecompile[deployerallowlist.ConfigKey]
if precompileConfig != nil {
deployerAllowListConfig := precompileConfig.(*deployerallowlist.Config)
for _, address := range []string{teleporterAddress, teleporterMessengerDeployerAddress} {
Expand Down Expand Up @@ -179,34 +178,35 @@ func addAddressToAllowed(
}

func getPrecompiles(
config *params.ChainConfig,
params SubnetEVMGenesisParams,
subnetEVMGenesisParams SubnetEVMGenesisParams,
genesisTimestamp *uint64,
) {
if params.enableWarpPrecompile {
) params.Precompiles {
precompiles := make(params.Precompiles)
if subnetEVMGenesisParams.enableWarpPrecompile {
warpConfig := configureWarp(genesisTimestamp)
config.GenesisPrecompiles[warp.ConfigKey] = &warpConfig
precompiles[warp.ConfigKey] = &warpConfig
}

if params.enableNativeMinterPrecompile {
mintConfig := configureNativeMinter(params, genesisTimestamp)
config.GenesisPrecompiles[nativeminter.ConfigKey] = &mintConfig
if subnetEVMGenesisParams.enableNativeMinterPrecompile {
mintConfig := configureNativeMinter(subnetEVMGenesisParams, genesisTimestamp)
precompiles[nativeminter.ConfigKey] = &mintConfig
}

if params.enableContractDeployerPrecompile {
contractConfig := configureContractDeployerAllowList(params, genesisTimestamp)
config.GenesisPrecompiles[deployerallowlist.ConfigKey] = &contractConfig
if subnetEVMGenesisParams.enableContractDeployerPrecompile {
contractConfig := configureContractDeployerAllowList(subnetEVMGenesisParams, genesisTimestamp)
precompiles[deployerallowlist.ConfigKey] = &contractConfig
}
if params.enableTransactionPrecompile {
txConfig := configureTransactionAllowList(params, genesisTimestamp)
config.GenesisPrecompiles[txallowlist.ConfigKey] = &txConfig
if subnetEVMGenesisParams.enableTransactionPrecompile {
txConfig := configureTransactionAllowList(subnetEVMGenesisParams, genesisTimestamp)
precompiles[txallowlist.ConfigKey] = &txConfig
}
if params.enableFeeManagerPrecompile {
feeConfig := configureFeeManager(params, genesisTimestamp)
config.GenesisPrecompiles[feemanager.ConfigKey] = &feeConfig
if subnetEVMGenesisParams.enableFeeManagerPrecompile {
feeConfig := configureFeeManager(subnetEVMGenesisParams, genesisTimestamp)
precompiles[feemanager.ConfigKey] = &feeConfig
}
if params.enableRewardManagerPrecompile {
rewardManagerConfig := configureRewardManager(params, genesisTimestamp)
config.GenesisPrecompiles[rewardmanager.ConfigKey] = &rewardManagerConfig
if subnetEVMGenesisParams.enableRewardManagerPrecompile {
rewardManagerConfig := configureRewardManager(subnetEVMGenesisParams, genesisTimestamp)
precompiles[rewardmanager.ConfigKey] = &rewardManagerConfig
}
return precompiles
}
Loading

0 comments on commit 900c578

Please sign in to comment.