From af1a9e38dad287bd7ec8a83d55b7a58b60ea8367 Mon Sep 17 00:00:00 2001 From: pingke Date: Mon, 15 Jan 2024 14:58:37 +0800 Subject: [PATCH 1/5] update according to contract event change --- cmd/dashboard/main.go | 18 +++--------------- ethstorage/eth/polling_client.go | 2 +- 2 files changed, 4 insertions(+), 16 deletions(-) diff --git a/cmd/dashboard/main.go b/cmd/dashboard/main.go index 3eddff20..f7daabf1 100644 --- a/cmd/dashboard/main.go +++ b/cmd/dashboard/main.go @@ -183,26 +183,14 @@ func (d *dashboard) FetchMiningEvents(start, end uint64) ([]*miningEvent, uint64 return nil, start, fmt.Errorf("GetTransactionByHash fail, tx hash: %s, error: %s", l.TxHash.Hex(), err.Error()) } - // TODO: update when new version contract deployed, use the reward in the new MinedBlock event - balance, nErr := d.l1Source.BalanceAt(d.ctx, d.l1Contract, new(big.Int).SetUint64(l.BlockNumber)) - if nErr != nil { - return nil, start, fmt.Errorf("BalanceAt fail, block: %d, error: %s", l.BlockNumber, err.Error()) - } - balanceBefore, oErr := d.l1Source.BalanceAt(d.ctx, d.l1Contract, new(big.Int).SetUint64(l.BlockNumber-1)) - if oErr != nil { - return nil, start, fmt.Errorf("BalanceAt fail, block: %d, error: %s", l.BlockNumber-1, err.Error()) - } - reward := new(big.Int).Sub(balanceBefore, balance).Uint64() * 99 / 100 // reward = balance diff * 99% because 1% goes to the treasury - events = append(events, &miningEvent{ ShardId: new(big.Int).SetBytes(l.Topics[1].Bytes()).Uint64(), Difficulty: new(big.Int).SetBytes(l.Topics[2].Bytes()), BlockMined: new(big.Int).SetBytes(l.Topics[3].Bytes()), LastMineTime: new(big.Int).SetBytes(l.Data[:32]).Uint64(), - // TODO: update when new version contract deployed, use the miner in the new MinedBlock event - Miner: common.BytesToAddress(tx.Data()[80:100]), - Reward: reward / 10000000000, - GasFee: tx.Gas() * tx.GasPrice().Uint64() / 10000000000, + Miner: common.BytesToAddress(l.Data[:32][44:64]), + Reward: new(big.Int).SetBytes(l.Data[64:96]).Uint64(), + GasFee: tx.Gas() * tx.GasPrice().Uint64() / 10000000000, }) } return events, end + 1, nil diff --git a/ethstorage/eth/polling_client.go b/ethstorage/eth/polling_client.go index 73b35986..1f01995a 100644 --- a/ethstorage/eth/polling_client.go +++ b/ethstorage/eth/polling_client.go @@ -22,7 +22,7 @@ import ( const ( PutBlobEvent = "PutBlob(uint256,uint256,bytes32)" - MinedBlockEvent = "MinedBlock(uint256,uint256,uint256,uint256)" // TODO: update when new version contract deployed + MinedBlockEvent = "MinedBlock(uint256,uint256,uint256,uint256,address,uint256)" ) var httpRegex = regexp.MustCompile("^http(s)?://") From 8c5b2d8c115da1a63736b924a0cfeddbc0f3f46b Mon Sep 17 00:00:00 2001 From: pingke Date: Mon, 15 Jan 2024 17:08:19 +0800 Subject: [PATCH 2/5] change reward unit --- cmd/dashboard/main.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/dashboard/main.go b/cmd/dashboard/main.go index f7daabf1..85bb0caf 100644 --- a/cmd/dashboard/main.go +++ b/cmd/dashboard/main.go @@ -189,7 +189,7 @@ func (d *dashboard) FetchMiningEvents(start, end uint64) ([]*miningEvent, uint64 BlockMined: new(big.Int).SetBytes(l.Topics[3].Bytes()), LastMineTime: new(big.Int).SetBytes(l.Data[:32]).Uint64(), Miner: common.BytesToAddress(l.Data[:32][44:64]), - Reward: new(big.Int).SetBytes(l.Data[64:96]).Uint64(), + Reward: new(big.Int).SetBytes(l.Data[64:96]).Uint64() / 10000000000, GasFee: tx.Gas() * tx.GasPrice().Uint64() / 10000000000, }) } From 0bee40c2fb639ce554625e86449a7a7a86332208 Mon Sep 17 00:00:00 2001 From: pingke Date: Mon, 15 Jan 2024 17:42:41 +0800 Subject: [PATCH 3/5] update --- ethstorage/metrics/metrics.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/ethstorage/metrics/metrics.go b/ethstorage/metrics/metrics.go index bc0a4184..6315a8d3 100644 --- a/ethstorage/metrics/metrics.go +++ b/ethstorage/metrics/metrics.go @@ -463,11 +463,11 @@ func (m *Metrics) SetLastKVIndexAndMaxShardId(lastL1Block, lastKVIndex uint64, m } func (m *Metrics) SetMiningInfo(shardId uint64, difficulty, minedTime, blockMined uint64, miner common.Address, gasFee, reward uint64) { + m.Difficulties.WithLabelValues(fmt.Sprintf("%d", shardId)).Set(float64(difficulty)) + m.BlockMined.WithLabelValues(fmt.Sprintf("%d", shardId)).Set(float64(blockMined)) + if t, ok := m.lastSubmissionTimes[shardId]; ok && t <= minedTime { - m.Difficulties.WithLabelValues(fmt.Sprintf("%d", shardId)).Set(float64(difficulty)) m.LastSubmissionTime.WithLabelValues(fmt.Sprintf("%d", shardId)).Set(float64(minedTime)) - m.BlockMined.WithLabelValues(fmt.Sprintf("%d", shardId)).Set(float64(blockMined)) - m.LastMinerSubmissionTime.WithLabelValues(fmt.Sprintf("%d", shardId), miner.Hex(), fmt.Sprintf("%d", blockMined)).Set(float64(minedTime)) m.GasFee.WithLabelValues(fmt.Sprintf("%d", shardId), miner.Hex(), fmt.Sprintf("%d", blockMined)).Set(float64(gasFee)) m.MiningReward.WithLabelValues(fmt.Sprintf("%d", shardId), miner.Hex(), fmt.Sprintf("%d", blockMined)).Set(float64(reward)) From 3efa05acb139286c299b32d3158899c0e1bc16e8 Mon Sep 17 00:00:00 2001 From: pingke Date: Mon, 15 Jan 2024 23:24:11 +0800 Subject: [PATCH 4/5] add init value for metrics --- cmd/dashboard/main.go | 19 ++++++++++++++++++- ethstorage/eth/polling_client.go | 4 ++-- ethstorage/metrics/metrics.go | 6 +++--- ethstorage/miner/l1_mining_api.go | 2 +- integration_tests/node_mine_test.go | 26 +++++++++++++------------- 5 files changed, 37 insertions(+), 20 deletions(-) diff --git a/cmd/dashboard/main.go b/cmd/dashboard/main.go index 85bb0caf..0bf25736 100644 --- a/cmd/dashboard/main.go +++ b/cmd/dashboard/main.go @@ -207,6 +207,20 @@ func (d *dashboard) GetTransactionByHash(hash common.Hash) (*types.Transaction, return tx, nil } +func (d *dashboard) InitMetrics() error { + lastMineTimeVal, err := d.l1Source.ReadContractField("prepaidLastMineTime", new(big.Int).SetUint64(d.startBlock)) + if err != nil { + return err + } + minDiffVal, err := d.l1Source.ReadContractField("minimumDiff", new(big.Int).SetUint64(d.startBlock)) + if err != nil { + return err + } + d.m.SetMiningInfo(0, new(big.Int).SetBytes(minDiffVal).Uint64(), new(big.Int).SetBytes(lastMineTimeVal).Uint64(), + 0, common.Address{}, 0, 0) + return nil +} + func readSlotFromContract(ctx context.Context, client *ethclient.Client, l1Contract common.Address, fieldName string) ([]byte, error) { h := crypto.Keccak256Hash([]byte(fieldName + "()")) msg := ethereum.CallMsg{ @@ -242,7 +256,10 @@ func main() { if err != nil { log.Crit("New dashboard fail", "err", err) } - + err = d.InitMetrics() + if err != nil { + log.Crit("Init metrics value fail", "err", err.Error()) + } l1LatestBlockSub := eth.PollBlockChanges(d.ctx, d.logger, d.l1Source, d.RefreshMetrics, ethRPC.LatestBlockNumber, epoch, epoch) defer l1LatestBlockSub.Unsubscribe() diff --git a/ethstorage/eth/polling_client.go b/ethstorage/eth/polling_client.go index 1f01995a..0760c590 100644 --- a/ethstorage/eth/polling_client.go +++ b/ethstorage/eth/polling_client.go @@ -276,13 +276,13 @@ func (w *PollingClient) GetKvMetas(kvIndices []uint64, blockNumber int64) ([][32 return res[0].([][32]byte), nil } -func (w *PollingClient) ReadContractField(fieldName string) ([]byte, error) { +func (w *PollingClient) ReadContractField(fieldName string, blockNumber *big.Int) ([]byte, error) { h := crypto.Keccak256Hash([]byte(fieldName + "()")) msg := ethereum.CallMsg{ To: &w.esContract, Data: h[0:4], } - bs, err := w.Client.CallContract(context.Background(), msg, nil) + bs, err := w.Client.CallContract(context.Background(), msg, blockNumber) if err != nil { return nil, fmt.Errorf("failed to get %s from contract: %v", fieldName, err) } diff --git a/ethstorage/metrics/metrics.go b/ethstorage/metrics/metrics.go index 6315a8d3..bc0a4184 100644 --- a/ethstorage/metrics/metrics.go +++ b/ethstorage/metrics/metrics.go @@ -463,11 +463,11 @@ func (m *Metrics) SetLastKVIndexAndMaxShardId(lastL1Block, lastKVIndex uint64, m } func (m *Metrics) SetMiningInfo(shardId uint64, difficulty, minedTime, blockMined uint64, miner common.Address, gasFee, reward uint64) { - m.Difficulties.WithLabelValues(fmt.Sprintf("%d", shardId)).Set(float64(difficulty)) - m.BlockMined.WithLabelValues(fmt.Sprintf("%d", shardId)).Set(float64(blockMined)) - if t, ok := m.lastSubmissionTimes[shardId]; ok && t <= minedTime { + m.Difficulties.WithLabelValues(fmt.Sprintf("%d", shardId)).Set(float64(difficulty)) m.LastSubmissionTime.WithLabelValues(fmt.Sprintf("%d", shardId)).Set(float64(minedTime)) + m.BlockMined.WithLabelValues(fmt.Sprintf("%d", shardId)).Set(float64(blockMined)) + m.LastMinerSubmissionTime.WithLabelValues(fmt.Sprintf("%d", shardId), miner.Hex(), fmt.Sprintf("%d", blockMined)).Set(float64(minedTime)) m.GasFee.WithLabelValues(fmt.Sprintf("%d", shardId), miner.Hex(), fmt.Sprintf("%d", blockMined)).Set(float64(gasFee)) m.MiningReward.WithLabelValues(fmt.Sprintf("%d", shardId), miner.Hex(), fmt.Sprintf("%d", blockMined)).Set(float64(reward)) diff --git a/ethstorage/miner/l1_mining_api.go b/ethstorage/miner/l1_mining_api.go index ada2f3e6..14c7edfd 100644 --- a/ethstorage/miner/l1_mining_api.go +++ b/ethstorage/miner/l1_mining_api.go @@ -212,7 +212,7 @@ func (m *l1MiningAPI) estimateReward(ctx context.Context, cfg Config, contract c } lastMineTime := info.LastMineTime - plmt, err := m.ReadContractField("prepaidLastMineTime") + plmt, err := m.ReadContractField("prepaidLastMineTime", nil) if err != nil { m.lg.Error("Failed to read prepaidLastMineTime", "error", err.Error()) return nil, err diff --git a/integration_tests/node_mine_test.go b/integration_tests/node_mine_test.go index 28df21aa..d90659b6 100644 --- a/integration_tests/node_mine_test.go +++ b/integration_tests/node_mine_test.go @@ -186,13 +186,13 @@ func waitForMined(l1api miner.L1API, contract common.Address, chainHeadCh chan e } func initStorageConfig(t *testing.T, client *eth.PollingClient, l1Contract, miner common.Address) *storage.StorageConfig { - result, err := client.ReadContractField("maxKvSizeBits") + result, err := client.ReadContractField("maxKvSizeBits", nil) if err != nil { t.Fatal("get maxKvSizeBits", err) } maxKvSizeBits := new(big.Int).SetBytes(result).Uint64() chunkSizeBits := maxKvSizeBits - result, err = client.ReadContractField("shardEntryBits") + result, err = client.ReadContractField("shardEntryBits", nil) if err != nil { t.Fatal("get shardEntryBits", err) } @@ -347,58 +347,58 @@ func initMiningConfig(t *testing.T, l1Contract common.Address, client *eth.Polli } miningConfig.SignerFnFactory = factory miningConfig.SignerAddr = addrFrom - result, err := client.ReadContractField("randomChecks") + result, err := client.ReadContractField("randomChecks", nil) if err != nil { t.Fatal("get randomChecks", err) } miningConfig.RandomChecks = new(big.Int).SetBytes(result).Uint64() - result, err = client.ReadContractField("nonceLimit") + result, err = client.ReadContractField("nonceLimit", nil) if err != nil { t.Fatal("get nonceLimit", err) } miningConfig.NonceLimit = new(big.Int).SetBytes(result).Uint64() - result, err = client.ReadContractField("minimumDiff") + result, err = client.ReadContractField("minimumDiff", nil) if err != nil { t.Fatal("get minimumDiff", err) } miningConfig.MinimumDiff = new(big.Int).SetBytes(result) - result, err = client.ReadContractField("cutoff") + result, err = client.ReadContractField("cutoff", nil) if err != nil { t.Fatal("get cutoff", err) } miningConfig.Cutoff = new(big.Int).SetBytes(result) - result, err = client.ReadContractField("diffAdjDivisor") + result, err = client.ReadContractField("diffAdjDivisor", nil) if err != nil { t.Fatal("get diffAdjDivisor", err) } miningConfig.DiffAdjDivisor = new(big.Int).SetBytes(result) - result, err = client.ReadContractField("dcfFactor") + result, err = client.ReadContractField("dcfFactor", nil) if err != nil { t.Fatal("get dcfFactor", err) } miningConfig.DcfFactor = new(big.Int).SetBytes(result) - result, err = client.ReadContractField("startTime") + result, err = client.ReadContractField("startTime", nil) if err != nil { t.Fatal("get startTime", err) } miningConfig.StartTime = new(big.Int).SetBytes(result).Uint64() - result, err = client.ReadContractField("shardEntryBits") + result, err = client.ReadContractField("shardEntryBits", nil) if err != nil { t.Fatal("get shardEntryBits", err) } miningConfig.ShardEntry = 1 << new(big.Int).SetBytes(result).Uint64() - result, err = client.ReadContractField("treasuryShare") + result, err = client.ReadContractField("treasuryShare", nil) if err != nil { t.Fatal("get treasuryShare", err) } miningConfig.TreasuryShare = new(big.Int).SetBytes(result).Uint64() - result, err = client.ReadContractField("storageCost") + result, err = client.ReadContractField("storageCost", nil) if err != nil { t.Fatal("get storageCost", err) } miningConfig.StorageCost = new(big.Int).SetBytes(result) - result, err = client.ReadContractField("prepaidAmount") + result, err = client.ReadContractField("prepaidAmount", nil) if err != nil { t.Fatal("get prepaidAmount", err) } From 174037ea97e770b64235ddc908b6480766865d42 Mon Sep 17 00:00:00 2001 From: pingke Date: Fri, 19 Jan 2024 16:03:54 +0800 Subject: [PATCH 5/5] resolve comments --- cmd/dashboard/main.go | 31 +++---------------------------- 1 file changed, 3 insertions(+), 28 deletions(-) diff --git a/cmd/dashboard/main.go b/cmd/dashboard/main.go index 0bf25736..c45275cd 100644 --- a/cmd/dashboard/main.go +++ b/cmd/dashboard/main.go @@ -13,12 +13,9 @@ import ( "path/filepath" "time" - "github.com/ethereum/go-ethereum" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/core/rawdb" "github.com/ethereum/go-ethereum/core/types" - "github.com/ethereum/go-ethereum/crypto" - "github.com/ethereum/go-ethereum/ethclient" "github.com/ethereum/go-ethereum/ethdb" "github.com/ethereum/go-ethereum/log" ethRPC "github.com/ethereum/go-ethereum/rpc" @@ -107,10 +104,11 @@ func newDashboard(rpcURL string, l1Contract common.Address) (*dashboard, error) } } - shardEntryBits, err := readUintFromContract(ctx, l1.Client, l1Contract, "shardEntryBits") + result, err := l1.ReadContractField("shardEntryBits", nil) if err != nil { return nil, err } + shardEntryBits := new(big.Int).SetBytes(result).Uint64() return &dashboard{ ctx: ctx, @@ -188,7 +186,7 @@ func (d *dashboard) FetchMiningEvents(start, end uint64) ([]*miningEvent, uint64 Difficulty: new(big.Int).SetBytes(l.Topics[2].Bytes()), BlockMined: new(big.Int).SetBytes(l.Topics[3].Bytes()), LastMineTime: new(big.Int).SetBytes(l.Data[:32]).Uint64(), - Miner: common.BytesToAddress(l.Data[:32][44:64]), + Miner: common.BytesToAddress(l.Data[44:64]), Reward: new(big.Int).SetBytes(l.Data[64:96]).Uint64() / 10000000000, GasFee: tx.Gas() * tx.GasPrice().Uint64() / 10000000000, }) @@ -221,29 +219,6 @@ func (d *dashboard) InitMetrics() error { return nil } -func readSlotFromContract(ctx context.Context, client *ethclient.Client, l1Contract common.Address, fieldName string) ([]byte, error) { - h := crypto.Keccak256Hash([]byte(fieldName + "()")) - msg := ethereum.CallMsg{ - To: &l1Contract, - Data: h[0:4], - } - bs, err := client.CallContract(ctx, msg, nil) - if err != nil { - return nil, fmt.Errorf("failed to get %s from contract: %v", fieldName, err) - } - return bs, nil -} - -func readUintFromContract(ctx context.Context, client *ethclient.Client, l1Contract common.Address, fieldName string) (uint64, error) { - bs, err := readSlotFromContract(ctx, client, l1Contract, fieldName) - if err != nil { - return 0, err - } - value := new(big.Int).SetBytes(bs).Uint64() - log.Info("Read uint from contract", "field", fieldName, "value", value) - return value, nil -} - func main() { // Parse the flags and set up the logger to print everything requested flag.Parse()