From bcba70ec96b2e161bbbd5a78c1a9559852b37f27 Mon Sep 17 00:00:00 2001 From: egonspace Date: Tue, 21 May 2024 17:17:47 +0900 Subject: [PATCH 1/3] feat: add testable private chain for brioche --- core/genesis.go | 8 ++- params/config.go | 30 +++++++++ wemix/reward_monitor_test.go | 120 +++++++++++++++++++++++++++++++++++ 3 files changed, 157 insertions(+), 1 deletion(-) create mode 100644 wemix/reward_monitor_test.go diff --git a/core/genesis.go b/core/genesis.go index b4a318727a69..c9c90f189007 100644 --- a/core/genesis.go +++ b/core/genesis.go @@ -302,7 +302,11 @@ func SetupGenesisBlockWithOverride(db ethdb.Database, genesis *Genesis, override // chain config as that would be AllProtocolChanges (applying any new fork // on top of an existing private network genesis block). In that case, only // apply the overrides. - if genesis == nil && !(stored == params.MainnetGenesisHash || stored == params.WemixMainnetGenesisHash) { + if genesis == nil && + !(stored == params.MainnetGenesisHash || + stored == params.WemixMainnetGenesisHash || + stored == params.WemixTestnetGenesisHash || + stored == params.LocalPrivateHash) { newcfg = storedcfg if overrideArrowGlacier != nil { newcfg.ArrowGlacierBlock = overrideArrowGlacier @@ -327,6 +331,8 @@ func SetupGenesisBlockWithOverride(db ethdb.Database, genesis *Genesis, override func (g *Genesis) configOrDefault(ghash common.Hash) *params.ChainConfig { switch { + case ghash == params.LocalPrivateHash: + return params.WemixPrivateChainConfig case ghash == params.WemixMainnetGenesisHash: return params.WemixMainnetChainConfig case ghash == params.WemixTestnetGenesisHash: diff --git a/params/config.go b/params/config.go index 1bdd1807064c..9ac0fe3bf5ab 100644 --- a/params/config.go +++ b/params/config.go @@ -35,6 +35,7 @@ var ( RinkebyGenesisHash = common.HexToHash("0x6341fd3daf94b748c72ced5a5b26028f2474f5f00d824504e4fa37a75767e177") GoerliGenesisHash = common.HexToHash("0xbf7e331f7f7c1dd2e05159666b3bf8bc7a8a3a9eb1d518969eab529dd9b88c1a") KilnGenesisHash = common.HexToHash("0x51c7fe41be669f69c45c33a56982cbde405313342d9e2b00d7c91a7b284dd4f8") + LocalPrivateHash = common.HexToHash("0x288ae65911434238a06c2630ebcee93977c4e209fb7566542f2124ad6cf86536") // modify it for your local chain ) // TrustedCheckpoints associates each known checkpoint with the genesis hash of @@ -201,6 +202,35 @@ var ( }, } + WemixPrivateChainConfig = &ChainConfig{ + ChainID: big.NewInt(1111), + HomesteadBlock: big.NewInt(0), + DAOForkBlock: big.NewInt(0), + DAOForkSupport: true, + EIP150Block: big.NewInt(0), + EIP155Block: big.NewInt(0), + EIP158Block: big.NewInt(0), + ByzantiumBlock: big.NewInt(0), + ConstantinopleBlock: big.NewInt(0), + PetersburgBlock: big.NewInt(0), + IstanbulBlock: big.NewInt(0), + MuirGlacierBlock: big.NewInt(0), + BerlinBlock: big.NewInt(0), + LondonBlock: big.NewInt(0), + PangyoBlock: big.NewInt(0), + ApplepieBlock: big.NewInt(4600), + BriocheBlock: big.NewInt(4600), + Ethash: new(EthashConfig), + Brioche: &BriocheConfig{ + BlockReward: big.NewInt(1e17), + FirstHalvingBlock: big.NewInt(4700), + HalvingPeriod: big.NewInt(10), + FinishRewardBlock: big.NewInt(4900), // TODO fix last reward block + HalvingTimes: 10, + HalvingRate: 50, + }, + } + // SepoliaChainConfig contains the chain parameters to run a node on the Sepolia test network. SepoliaChainConfig = &ChainConfig{ ChainID: big.NewInt(11155111), diff --git a/wemix/reward_monitor_test.go b/wemix/reward_monitor_test.go new file mode 100644 index 000000000000..ddc6d9d2e1c3 --- /dev/null +++ b/wemix/reward_monitor_test.go @@ -0,0 +1,120 @@ +package wemix + +import ( + "bytes" + "encoding/hex" + "encoding/json" + "io/ioutil" + "math/big" + "net/http" + "strings" + "testing" + "time" +) + +type Payload struct { + Jsonrpc string `json:"jsonrpc"` + Method string `json:"method"` + Params []interface{} `json:"params"` + ID int `json:"id"` +} + +type TxRes struct { + BlockHash string `json:"blockHash"` + BlockNumber string `json:"blockNumber"` + From string `json:"from"` + To string `json:"to"` + Gas string `json:"gas"` + GasPrice string `json:"gasPrice"` + Hash string `json:"hash"` + Input string `json:"input"` + TransactionIndex string `json:"transactionIndex"` + Type string `json:"type"` + Value string `json:"value"` + V string `json:"v"` + R string `json:"r"` + S string `json:"s"` +} + +type BlockRes struct { + Jsonrpc string `json:"jsonrpc"` + ID int `json:"id"` + Result struct { + BaseFeePerGas string `json:"baseFeePerGas"` + Difficulty string `json:"difficulty"` + ExtraData string `json:"extraData"` + GasLimit string `json:"gasLimit"` + GasUsed string `json:"gasUsed"` + Hash string `json:"hash"` + LogsBloom string `json:"logsBloom"` + Miner string `json:"miner"` + MixHash string `json:"mixHash"` + Nonce string `json:"nonce"` + Number string `json:"number"` + Rewards string `json:"rewards"` + ParentHash string `json:"parentHash"` + ReceiptsRoot string `json:"receiptsRoot"` + Sha3Uncles string `json:"sha3Uncles"` + Size string `json:"size"` + StateRoot string `json:"stateRoot"` + Timestamp string `json:"timestamp"` + TotalDifficulty string `json:"totalDifficulty"` + Transactions []TxRes `json:"transactions"` + TransactionsRoot string `json:"transactionsRoot"` + Uncles []string `json:"uncles"` + } `json:"result"` +} + +func GetBlock() (*BlockRes, error) { + data := Payload{ + Jsonrpc: "2.0", + Method: "eth_getBlockByNumber", + Params: []interface{}{"latest", true}, + ID: 1, + } + payloadBytes, err := json.Marshal(data) + if err != nil { + return nil, err + } + body := bytes.NewReader(payloadBytes) + + req, err := http.NewRequest("POST", "http://127.0.0.1:8588", body) + if err != nil { + return nil, err + } + req.Header.Set("Content-Type", "application/json") + + res, err := http.DefaultClient.Do(req) + if err != nil { + return nil, err + } + + defer res.Body.Close() + + bytes, _ := ioutil.ReadAll(res.Body) + var result BlockRes + err = json.Unmarshal(bytes, &result) + + return &result, nil +} + +func TestBlockStatistics(t *testing.T) { + for { + block, err := GetBlock() + if err != nil { + t.Errorf("error get block: %s", err) + break + } + num := new(big.Int) + num.SetString(strings.TrimPrefix(block.Result.Number, "0x"), 16) + + data, err := hex.DecodeString(strings.TrimPrefix(block.Result.Rewards, "0x")) + var reward []reward + if err = json.Unmarshal(data, &reward); err != nil { + t.Errorf(err.Error()) + } + t.Logf("Number = %v, Rewards = %v", num, reward) + + time.Sleep(time.Second) + } +} From b8182a925208b97cbc521e75419447a044a34543 Mon Sep 17 00:00:00 2001 From: egonspace Date: Tue, 28 May 2024 12:11:40 +0900 Subject: [PATCH 2/3] fix: apply devnet config --- core/genesis.go | 6 +++--- params/config.go | 20 ++++++++++---------- wemix/reward_monitor_test.go | 6 +++--- 3 files changed, 16 insertions(+), 16 deletions(-) diff --git a/core/genesis.go b/core/genesis.go index c9c90f189007..4a0f245464d9 100644 --- a/core/genesis.go +++ b/core/genesis.go @@ -306,7 +306,7 @@ func SetupGenesisBlockWithOverride(db ethdb.Database, genesis *Genesis, override !(stored == params.MainnetGenesisHash || stored == params.WemixMainnetGenesisHash || stored == params.WemixTestnetGenesisHash || - stored == params.LocalPrivateHash) { + stored == params.DevnetGenesisHash) { newcfg = storedcfg if overrideArrowGlacier != nil { newcfg.ArrowGlacierBlock = overrideArrowGlacier @@ -331,8 +331,8 @@ func SetupGenesisBlockWithOverride(db ethdb.Database, genesis *Genesis, override func (g *Genesis) configOrDefault(ghash common.Hash) *params.ChainConfig { switch { - case ghash == params.LocalPrivateHash: - return params.WemixPrivateChainConfig + case ghash == params.DevnetGenesisHash: + return params.WemixDevnetChainConfig case ghash == params.WemixMainnetGenesisHash: return params.WemixMainnetChainConfig case ghash == params.WemixTestnetGenesisHash: diff --git a/params/config.go b/params/config.go index 29b765494715..2bfab6860a9e 100644 --- a/params/config.go +++ b/params/config.go @@ -35,7 +35,7 @@ var ( RinkebyGenesisHash = common.HexToHash("0x6341fd3daf94b748c72ced5a5b26028f2474f5f00d824504e4fa37a75767e177") GoerliGenesisHash = common.HexToHash("0xbf7e331f7f7c1dd2e05159666b3bf8bc7a8a3a9eb1d518969eab529dd9b88c1a") KilnGenesisHash = common.HexToHash("0x51c7fe41be669f69c45c33a56982cbde405313342d9e2b00d7c91a7b284dd4f8") - LocalPrivateHash = common.HexToHash("0x288ae65911434238a06c2630ebcee93977c4e209fb7566542f2124ad6cf86536") // modify it for your local chain + DevnetGenesisHash = common.HexToHash("0x45ef4029e520fff2763b31821da9bf761ca73f243bb8e912f4d3004ce3598151") ) // TrustedCheckpoints associates each known checkpoint with the genesis hash of @@ -202,8 +202,8 @@ var ( }, } - WemixPrivateChainConfig = &ChainConfig{ - ChainID: big.NewInt(1111), + WemixDevnetChainConfig = &ChainConfig{ + ChainID: big.NewInt(1113), HomesteadBlock: big.NewInt(0), DAOForkBlock: big.NewInt(0), DAOForkSupport: true, @@ -218,15 +218,15 @@ var ( BerlinBlock: big.NewInt(0), LondonBlock: big.NewInt(0), PangyoBlock: big.NewInt(0), - ApplepieBlock: big.NewInt(4600), - BriocheBlock: big.NewInt(4600), + ApplepieBlock: big.NewInt(0), + BriocheBlock: big.NewInt(607_147), // target date: 2024-05-29 16:00:00 (GMT+09) Ethash: new(EthashConfig), Brioche: &BriocheConfig{ - BlockReward: big.NewInt(1e17), - FirstHalvingBlock: big.NewInt(4700), - HalvingPeriod: big.NewInt(10), - FinishRewardBlock: big.NewInt(4900), // TODO fix last reward block - HalvingTimes: 10, + BlockReward: big.NewInt(1e18), + FirstHalvingBlock: big.NewInt(607_147), + HalvingPeriod: big.NewInt(63_115_200), + FinishRewardBlock: big.NewInt(2_412_358_747), // target date: 2100-11-01 11:00:00 (GMT+09) + HalvingTimes: 16, HalvingRate: 50, }, } diff --git a/wemix/reward_monitor_test.go b/wemix/reward_monitor_test.go index ddc6d9d2e1c3..e7ccdf6f3af9 100644 --- a/wemix/reward_monitor_test.go +++ b/wemix/reward_monitor_test.go @@ -78,7 +78,7 @@ func GetBlock() (*BlockRes, error) { } body := bytes.NewReader(payloadBytes) - req, err := http.NewRequest("POST", "http://127.0.0.1:8588", body) + req, err := http.NewRequest("POST", "http://172.207.48.146:8588", body) if err != nil { return nil, err } @@ -93,7 +93,7 @@ func GetBlock() (*BlockRes, error) { bytes, _ := ioutil.ReadAll(res.Body) var result BlockRes - err = json.Unmarshal(bytes, &result) + _ = json.Unmarshal(bytes, &result) return &result, nil } @@ -108,7 +108,7 @@ func TestBlockStatistics(t *testing.T) { num := new(big.Int) num.SetString(strings.TrimPrefix(block.Result.Number, "0x"), 16) - data, err := hex.DecodeString(strings.TrimPrefix(block.Result.Rewards, "0x")) + data, _ := hex.DecodeString(strings.TrimPrefix(block.Result.Rewards, "0x")) var reward []reward if err = json.Unmarshal(data, &reward); err != nil { t.Errorf(err.Error()) From d3d1ba45ddf8404b99a899e65ba54780c734b802 Mon Sep 17 00:00:00 2001 From: egonspace Date: Tue, 4 Jun 2024 11:37:05 +0900 Subject: [PATCH 3/3] fix: remove local test --- wemix/reward_monitor_test.go | 120 ----------------------------------- 1 file changed, 120 deletions(-) delete mode 100644 wemix/reward_monitor_test.go diff --git a/wemix/reward_monitor_test.go b/wemix/reward_monitor_test.go deleted file mode 100644 index e7ccdf6f3af9..000000000000 --- a/wemix/reward_monitor_test.go +++ /dev/null @@ -1,120 +0,0 @@ -package wemix - -import ( - "bytes" - "encoding/hex" - "encoding/json" - "io/ioutil" - "math/big" - "net/http" - "strings" - "testing" - "time" -) - -type Payload struct { - Jsonrpc string `json:"jsonrpc"` - Method string `json:"method"` - Params []interface{} `json:"params"` - ID int `json:"id"` -} - -type TxRes struct { - BlockHash string `json:"blockHash"` - BlockNumber string `json:"blockNumber"` - From string `json:"from"` - To string `json:"to"` - Gas string `json:"gas"` - GasPrice string `json:"gasPrice"` - Hash string `json:"hash"` - Input string `json:"input"` - TransactionIndex string `json:"transactionIndex"` - Type string `json:"type"` - Value string `json:"value"` - V string `json:"v"` - R string `json:"r"` - S string `json:"s"` -} - -type BlockRes struct { - Jsonrpc string `json:"jsonrpc"` - ID int `json:"id"` - Result struct { - BaseFeePerGas string `json:"baseFeePerGas"` - Difficulty string `json:"difficulty"` - ExtraData string `json:"extraData"` - GasLimit string `json:"gasLimit"` - GasUsed string `json:"gasUsed"` - Hash string `json:"hash"` - LogsBloom string `json:"logsBloom"` - Miner string `json:"miner"` - MixHash string `json:"mixHash"` - Nonce string `json:"nonce"` - Number string `json:"number"` - Rewards string `json:"rewards"` - ParentHash string `json:"parentHash"` - ReceiptsRoot string `json:"receiptsRoot"` - Sha3Uncles string `json:"sha3Uncles"` - Size string `json:"size"` - StateRoot string `json:"stateRoot"` - Timestamp string `json:"timestamp"` - TotalDifficulty string `json:"totalDifficulty"` - Transactions []TxRes `json:"transactions"` - TransactionsRoot string `json:"transactionsRoot"` - Uncles []string `json:"uncles"` - } `json:"result"` -} - -func GetBlock() (*BlockRes, error) { - data := Payload{ - Jsonrpc: "2.0", - Method: "eth_getBlockByNumber", - Params: []interface{}{"latest", true}, - ID: 1, - } - payloadBytes, err := json.Marshal(data) - if err != nil { - return nil, err - } - body := bytes.NewReader(payloadBytes) - - req, err := http.NewRequest("POST", "http://172.207.48.146:8588", body) - if err != nil { - return nil, err - } - req.Header.Set("Content-Type", "application/json") - - res, err := http.DefaultClient.Do(req) - if err != nil { - return nil, err - } - - defer res.Body.Close() - - bytes, _ := ioutil.ReadAll(res.Body) - var result BlockRes - _ = json.Unmarshal(bytes, &result) - - return &result, nil -} - -func TestBlockStatistics(t *testing.T) { - for { - block, err := GetBlock() - if err != nil { - t.Errorf("error get block: %s", err) - break - } - num := new(big.Int) - num.SetString(strings.TrimPrefix(block.Result.Number, "0x"), 16) - - data, _ := hex.DecodeString(strings.TrimPrefix(block.Result.Rewards, "0x")) - var reward []reward - if err = json.Unmarshal(data, &reward); err != nil { - t.Errorf(err.Error()) - } - t.Logf("Number = %v, Rewards = %v", num, reward) - - time.Sleep(time.Second) - } -}