github.com/unicornultrafoundation/go-u2u@v1.0.0-rc1.0.20240205080301-e74a83d3fadc/u2u/rules.go (about)

     1  package u2u
     2  
     3  import (
     4  	"encoding/json"
     5  	"math/big"
     6  	"time"
     7  
     8  	"github.com/unicornultrafoundation/go-helios/native/idx"
     9  	"github.com/unicornultrafoundation/go-u2u/common"
    10  	"github.com/unicornultrafoundation/go-u2u/core/vm"
    11  	ethparams "github.com/unicornultrafoundation/go-u2u/params"
    12  
    13  	"github.com/unicornultrafoundation/go-u2u/native"
    14  	"github.com/unicornultrafoundation/go-u2u/u2u/contracts/evmwriter"
    15  )
    16  
    17  const (
    18  	MainNetworkID   uint64 = 39
    19  	TestNetworkID   uint64 = 2484
    20  	FakeNetworkID   uint64 = 4439
    21  	DefaultEventGas uint64 = 28000
    22  	berlinBit              = 1 << 0
    23  	londonBit              = 1 << 1
    24  	llrBit                 = 1 << 2
    25  )
    26  
    27  var DefaultVMConfig = vm.Config{
    28  	StatePrecompiles: map[common.Address]vm.PrecompiledStateContract{
    29  		evmwriter.ContractAddress: &evmwriter.PreCompiledContract{},
    30  	},
    31  }
    32  
    33  type RulesRLP struct {
    34  	Name      string
    35  	NetworkID uint64
    36  
    37  	// Graph options
    38  	Dag DagRules
    39  
    40  	// Epochs options
    41  	Epochs EpochsRules
    42  
    43  	// Blockchain options
    44  	Blocks BlocksRules
    45  
    46  	// Economy options
    47  	Economy EconomyRules
    48  
    49  	Upgrades Upgrades `rlp:"-"`
    50  }
    51  
    52  // Rules describes u2u net.
    53  // Note keep track of all the non-copiable variables in Copy()
    54  type Rules RulesRLP
    55  
    56  // GasPowerRules defines gas power rules in the consensus.
    57  type GasPowerRules struct {
    58  	AllocPerSec        uint64
    59  	MaxAllocPeriod     native.Timestamp
    60  	StartupAllocPeriod native.Timestamp
    61  	MinStartupGas      uint64
    62  }
    63  
    64  type GasRulesRLPV1 struct {
    65  	MaxEventGas  uint64
    66  	EventGas     uint64
    67  	ParentGas    uint64
    68  	ExtraDataGas uint64
    69  	// Post-LLR fields
    70  	BlockVotesBaseGas    uint64
    71  	BlockVoteGas         uint64
    72  	EpochVoteGas         uint64
    73  	MisbehaviourProofGas uint64
    74  }
    75  
    76  type GasRules GasRulesRLPV1
    77  
    78  type EpochsRules struct {
    79  	MaxEpochGas      uint64
    80  	MaxEpochDuration native.Timestamp
    81  }
    82  
    83  // DagRules of Hashgraph DAG (directed acyclic graph).
    84  type DagRules struct {
    85  	MaxParents     idx.Event
    86  	MaxFreeParents idx.Event // maximum number of parents with no gas cost
    87  	MaxExtraData   uint32
    88  }
    89  
    90  // BlocksMissed is information about missed blocks from a staker
    91  type BlocksMissed struct {
    92  	BlocksNum idx.Block
    93  	Period    native.Timestamp
    94  }
    95  
    96  // EconomyRules contains economy constants
    97  type EconomyRules struct {
    98  	BlockMissedSlack idx.Block
    99  
   100  	Gas GasRules
   101  
   102  	MinGasPrice *big.Int
   103  
   104  	ShortGasPower GasPowerRules
   105  	LongGasPower  GasPowerRules
   106  }
   107  
   108  // BlocksRules contains blocks constants
   109  type BlocksRules struct {
   110  	MaxBlockGas             uint64 // technical hard limit, gas is mostly governed by gas power allocation
   111  	MaxEmptyBlockSkipPeriod native.Timestamp
   112  }
   113  
   114  type Upgrades struct {
   115  	Berlin bool
   116  	London bool
   117  	Llr    bool
   118  }
   119  
   120  type UpgradeHeight struct {
   121  	Upgrades Upgrades
   122  	Height   idx.Block
   123  }
   124  
   125  // EvmChainConfig returns ChainConfig for transactions signing and execution
   126  func (r Rules) EvmChainConfig(hh []UpgradeHeight) *ethparams.ChainConfig {
   127  	cfg := *ethparams.AllProtocolChanges
   128  	cfg.ChainID = new(big.Int).SetUint64(r.NetworkID)
   129  	cfg.BerlinBlock = nil
   130  	cfg.LondonBlock = nil
   131  	for i, h := range hh {
   132  		height := new(big.Int)
   133  		if i > 0 {
   134  			height.SetUint64(uint64(h.Height))
   135  		}
   136  		if cfg.BerlinBlock == nil && h.Upgrades.Berlin {
   137  			cfg.BerlinBlock = height
   138  		}
   139  		if !h.Upgrades.Berlin {
   140  			cfg.BerlinBlock = nil
   141  		}
   142  
   143  		if cfg.LondonBlock == nil && h.Upgrades.London {
   144  			cfg.LondonBlock = height
   145  		}
   146  		if !h.Upgrades.London {
   147  			cfg.LondonBlock = nil
   148  		}
   149  	}
   150  	return &cfg
   151  }
   152  
   153  func MainNetRules() Rules {
   154  	return Rules{
   155  		Name:      "main",
   156  		NetworkID: MainNetworkID,
   157  		Dag:       DefaultDagRules(),
   158  		Epochs:    DefaultEpochsRules(),
   159  		Economy:   DefaultEconomyRules(),
   160  		Blocks: BlocksRules{
   161  			MaxBlockGas:             20500000,
   162  			MaxEmptyBlockSkipPeriod: native.Timestamp(1 * time.Second),
   163  		},
   164  		Upgrades: Upgrades{
   165  			Berlin: true,
   166  			London: true,
   167  			Llr:    true,
   168  		},
   169  	}
   170  }
   171  
   172  func TestNetRules() Rules {
   173  	return Rules{
   174  		Name:      "test",
   175  		NetworkID: TestNetworkID,
   176  		Dag:       DefaultDagRules(),
   177  		Epochs:    DefaultEpochsRules(),
   178  		Economy:   DefaultEconomyRules(),
   179  		Blocks: BlocksRules{
   180  			MaxBlockGas:             20500000,
   181  			MaxEmptyBlockSkipPeriod: native.Timestamp(1 * time.Second),
   182  		},
   183  		Upgrades: Upgrades{
   184  			Berlin: true,
   185  			London: true,
   186  			Llr:    true,
   187  		},
   188  	}
   189  }
   190  
   191  func FakeNetRules() Rules {
   192  	return Rules{
   193  		Name:      "fake",
   194  		NetworkID: FakeNetworkID,
   195  		Dag:       DefaultDagRules(),
   196  		Epochs:    FakeNetEpochsRules(),
   197  		Economy:   FakeEconomyRules(),
   198  		Blocks: BlocksRules{
   199  			MaxBlockGas:             20500000,
   200  			MaxEmptyBlockSkipPeriod: native.Timestamp(3 * time.Second),
   201  		},
   202  		Upgrades: Upgrades{
   203  			Berlin: true,
   204  			London: true,
   205  			Llr:    true,
   206  		},
   207  	}
   208  }
   209  
   210  // DefaultEconomyRules returns mainnet economy
   211  func DefaultEconomyRules() EconomyRules {
   212  	return EconomyRules{
   213  		BlockMissedSlack: 50,
   214  		Gas:              DefaultGasRules(),
   215  		MinGasPrice:      big.NewInt(1e9),
   216  		ShortGasPower:    DefaultShortGasPowerRules(),
   217  		LongGasPower:     DefaulLongGasPowerRules(),
   218  	}
   219  }
   220  
   221  // FakeEconomyRules returns fakenet economy
   222  func FakeEconomyRules() EconomyRules {
   223  	cfg := DefaultEconomyRules()
   224  	cfg.ShortGasPower = FakeShortGasPowerRules()
   225  	cfg.LongGasPower = FakeLongGasPowerRules()
   226  	return cfg
   227  }
   228  
   229  func DefaultDagRules() DagRules {
   230  	return DagRules{
   231  		MaxParents:     10,
   232  		MaxFreeParents: 3,
   233  		MaxExtraData:   128,
   234  	}
   235  }
   236  
   237  func DefaultEpochsRules() EpochsRules {
   238  	return EpochsRules{
   239  		MaxEpochGas:      300000000,
   240  		MaxEpochDuration: native.Timestamp(7 * time.Minute),
   241  	}
   242  }
   243  
   244  func DefaultGasRules() GasRules {
   245  	return GasRules{
   246  		MaxEventGas:          10000000 + DefaultEventGas,
   247  		EventGas:             DefaultEventGas,
   248  		ParentGas:            2400,
   249  		ExtraDataGas:         25,
   250  		BlockVotesBaseGas:    1024,
   251  		BlockVoteGas:         512,
   252  		EpochVoteGas:         1536,
   253  		MisbehaviourProofGas: 71536,
   254  	}
   255  }
   256  
   257  func FakeNetEpochsRules() EpochsRules {
   258  	cfg := DefaultEpochsRules()
   259  	cfg.MaxEpochGas /= 5
   260  	cfg.MaxEpochDuration = native.Timestamp(5 * time.Second)
   261  	return cfg
   262  }
   263  
   264  // DefaulLongGasPowerRules is long-window config
   265  func DefaulLongGasPowerRules() GasPowerRules {
   266  	return GasPowerRules{
   267  		AllocPerSec:        100 * DefaultEventGas,
   268  		MaxAllocPeriod:     native.Timestamp(60 * time.Minute),
   269  		StartupAllocPeriod: native.Timestamp(5 * time.Second),
   270  		MinStartupGas:      DefaultEventGas * 20,
   271  	}
   272  }
   273  
   274  // DefaultShortGasPowerRules is short-window config
   275  func DefaultShortGasPowerRules() GasPowerRules {
   276  	// 2x faster allocation rate, 6x lower max accumulated gas power
   277  	cfg := DefaulLongGasPowerRules()
   278  	cfg.AllocPerSec *= 2
   279  	cfg.StartupAllocPeriod /= 2
   280  	cfg.MaxAllocPeriod /= 2 * 6
   281  	return cfg
   282  }
   283  
   284  // FakeLongGasPowerRules is fake long-window config
   285  func FakeLongGasPowerRules() GasPowerRules {
   286  	config := DefaulLongGasPowerRules()
   287  	config.AllocPerSec *= 1000
   288  	return config
   289  }
   290  
   291  // FakeShortGasPowerRules is fake short-window config
   292  func FakeShortGasPowerRules() GasPowerRules {
   293  	config := DefaultShortGasPowerRules()
   294  	config.AllocPerSec *= 1000
   295  	return config
   296  }
   297  
   298  func (r Rules) Copy() Rules {
   299  	cp := r
   300  	cp.Economy.MinGasPrice = new(big.Int).Set(r.Economy.MinGasPrice)
   301  	return cp
   302  }
   303  
   304  func (r Rules) String() string {
   305  	b, _ := json.Marshal(&r)
   306  	return string(b)
   307  }