diff --git a/cmd/dashboard/main.go b/cmd/dashboard/main.go index 3eddff20..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, @@ -183,26 +181,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[44:64]), + Reward: new(big.Int).SetBytes(l.Data[64:96]).Uint64() / 10000000000, + GasFee: tx.Gas() * tx.GasPrice().Uint64() / 10000000000, }) } return events, end + 1, nil @@ -219,27 +205,18 @@ func (d *dashboard) GetTransactionByHash(hash common.Hash) (*types.Transaction, return tx, 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) +func (d *dashboard) InitMetrics() error { + lastMineTimeVal, err := d.l1Source.ReadContractField("prepaidLastMineTime", new(big.Int).SetUint64(d.startBlock)) if err != nil { - return nil, fmt.Errorf("failed to get %s from contract: %v", fieldName, err) + return 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) + minDiffVal, err := d.l1Source.ReadContractField("minimumDiff", new(big.Int).SetUint64(d.startBlock)) if err != nil { - return 0, err + return err } - value := new(big.Int).SetBytes(bs).Uint64() - log.Info("Read uint from contract", "field", fieldName, "value", value) - return value, nil + d.m.SetMiningInfo(0, new(big.Int).SetBytes(minDiffVal).Uint64(), new(big.Int).SetBytes(lastMineTimeVal).Uint64(), + 0, common.Address{}, 0, 0) + return nil } func main() { @@ -254,7 +231,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 73b35986..0760c590 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)?://") @@ -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/miner/l1_mining_api.go b/ethstorage/miner/l1_mining_api.go index 0d7df0ce..32e8e70d 100644 --- a/ethstorage/miner/l1_mining_api.go +++ b/ethstorage/miner/l1_mining_api.go @@ -226,7 +226,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 4b984e31..abe33ea2 100644 --- a/integration_tests/node_mine_test.go +++ b/integration_tests/node_mine_test.go @@ -187,13 +187,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) } @@ -348,58 +348,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) }