Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hip30 balance migration with fix. #4502

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ trace-pointer:
bash ./scripts/go_executable_build.sh -t

debug:
rm -rf .dht-127.0.0.1*
bash ./test/debug.sh

debug-kill:
Expand Down Expand Up @@ -167,3 +168,6 @@ docker:

travis_go_checker:
bash ./scripts/travis_go_checker.sh

travis_rpc_checker:
bash ./scripts/travis_rpc_checker.sh
2 changes: 1 addition & 1 deletion consensus/consensus_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func (consensus *Consensus) updatePublicKeys(pubKeys, allowlist []bls_cosi.Publi
if len(allKeys) != 0 {
consensus.LeaderPubKey = &allKeys[0]
consensus.getLogger().Info().
Str("info", consensus.LeaderPubKey.Bytes.Hex()).Msg("My Leader")
Str("info", consensus.LeaderPubKey.Bytes.Hex()).Msg("Setting leader as first validator, because provided new keys")
} else {
consensus.getLogger().Error().
Msg("[UpdatePublicKeys] Participants is empty")
Expand Down
42 changes: 23 additions & 19 deletions core/preimages.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"os"
"strconv"

"github.com/ethereum/go-ethereum/crypto"

ethCommon "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
"github.com/harmony-one/harmony/api/service/prometheus"
Expand Down Expand Up @@ -142,26 +144,28 @@ func ExportPreimages(chain BlockChain, path string) error {
dbReader := chain.ChainDb()
for accountIterator.Next(true) {
// the leaf nodes of the MPT represent accounts
if accountIterator.Leaf() {
// the leaf key is the hashed address
hashed := accountIterator.LeafKey()
asHash := ethCommon.BytesToHash(hashed)
// obtain the corresponding address
preimage := rawdb.ReadPreimage(
dbReader, asHash,
)
if len(preimage) == 0 {
utils.Logger().Warn().
Msgf("Address not found for %x", asHash)
continue
}
address := ethCommon.BytesToAddress(preimage)
// key value format, so hash of value is first
csvWriter.Write([]string{
fmt.Sprintf("%x", asHash.Bytes()),
fmt.Sprintf("%x", address.Bytes()),
})
if !accountIterator.Leaf() {
continue
}
// the leaf key is the hashed address
hashed := accountIterator.LeafKey()
asHash := ethCommon.BytesToHash(hashed)
// obtain the corresponding address
preimage := rawdb.ReadPreimage(
dbReader, asHash,
)
if len(preimage) == 0 {
utils.Logger().Warn().
Msgf("Address not found for %x", asHash)
continue
}
address := ethCommon.BytesToAddress(preimage)
// key value format, so hash of value is first
csvWriter.Write([]string{
fmt.Sprintf("%x", asHash.Bytes()),
fmt.Sprintf("%x", address.Bytes()),
})

}
// lastly, write the block number
csvWriter.Write(
Expand Down
26 changes: 17 additions & 9 deletions core/state_processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,11 +141,11 @@ func (p *StateProcessor) Process(
gp, header, statedb, p.bc, p.config,
)
if err != nil {
if err == ErrNoMigrationPossible {
if errors.Is(err, ErrNoMigrationPossible) {
// ran out of accounts
processTxsAndStxs = false
}
if err != ErrNoMigrationRequired {
if !errors.Is(err, ErrNoMigrationRequired) {
return nil, nil, nil, nil, 0, nil, statedb, err
}
} else {
Expand Down Expand Up @@ -538,7 +538,7 @@ func MayBalanceMigration(
) (*types.CXReceipt, error) {
isMainnet := nodeconfig.GetDefaultConfig().GetNetworkType() == nodeconfig.Mainnet
if isMainnet {
if config.IsEpochBeforeHIP30(header.Epoch()) {
if config.IsOneEpochBeforeHIP30(header.Epoch()) {
nxtShards := shard.Schedule.InstanceForEpoch(
new(big.Int).Add(header.Epoch(), common.Big1),
).NumShards()
Expand Down Expand Up @@ -574,24 +574,30 @@ func MayBalanceMigration(
isDevnet := nodeconfig.GetDefaultConfig().GetNetworkType() == nodeconfig.Devnet
isLocalnet := nodeconfig.GetDefaultConfig().GetNetworkType() == nodeconfig.Localnet
if isDevnet || isLocalnet {
if config.IsEpochBeforeHIP30(header.Epoch()) {
if config.IsOneEpochBeforeHIP30(header.Epoch()) {
if myShard := chain.ShardID(); myShard != shard.BeaconChainShardID {
parentRoot := chain.GetBlockByHash(
parent := chain.GetBlockByHash(
header.ParentHash(),
).Root() // for examining MPT at this root, should exist
)
if parent == nil {
return nil, errors.Wrap(ErrNoMigrationPossible, "parent is nil")
}
parentRoot := parent.Root()
// for examining MPT at this root, should exist
cx, err := generateOneMigrationMessage(
db, parentRoot,
header.NumberU64(),
myShard, shard.BeaconChainShardID, // dstShard
)
if err != nil {
return nil, err
return nil, errors.Wrap(err, "generateOneMigrationMessage")
}
if cx != nil {
gasPool.SubGas(params.TxGasXShard)
return cx, nil
}
return nil, ErrNoMigrationPossible
//return nil, errors.Wrap(ErrNoMigrationPossible, "MayBalanceMigration: cx is nil")
return nil, nil
}
}
}
Expand Down Expand Up @@ -635,11 +641,13 @@ func generateOneMigrationMessage(
key := accountIterator.LeafKey()
preimage := rawdb.ReadPreimage(db, common.BytesToHash(key))
if len(preimage) == 0 {
return nil, errors.New(
e := errors.New(
fmt.Sprintf(
"cannot find preimage for %x", key,
),
)
fmt.Println(e)
continue
}
address := common.BytesToAddress(preimage)
// skip blank address
Expand Down
4 changes: 2 additions & 2 deletions internal/configs/sharding/localnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ const (
localnetV1Epoch = 1

localnetEpochBlock1 = 5
localnetBlocksPerEpoch = 64
localnetBlocksPerEpochV2 = 64
localnetBlocksPerEpoch = 8
localnetBlocksPerEpochV2 = 8
Comment on lines +34 to +35
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

reducing to 8 will impact the external external validator

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

16 ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

based on testing for external it had to be 64


localnetVdfDifficulty = 5000 // This takes about 10s to finish the vdf
)
Expand Down
5 changes: 3 additions & 2 deletions internal/params/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -797,8 +797,9 @@ func (c *ChainConfig) IsHIP30(epoch *big.Int) bool {

// During this epoch, shards 2 and 3 will start sending
// their balances over to shard 0 or 1.
func (c *ChainConfig) IsEpochBeforeHIP30(epoch *big.Int) bool {
return isForked(new(big.Int).Sub(c.HIP30Epoch, common.Big1), epoch)
func (c *ChainConfig) IsOneEpochBeforeHIP30(epoch *big.Int) bool {
return epoch.Sub(c.HIP30Epoch, epoch).Cmp(common.Big1) == 0
//return isForked(new(big.Int).Sub(c.HIP30Epoch, common.Big1), epoch)
}

// UpdateEthChainIDByShard update the ethChainID based on shard ID.
Expand Down
17 changes: 17 additions & 0 deletions internal/params/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package params

import (
"math/big"
"testing"

"github.com/stretchr/testify/require"
)

func TestIsOneEpochBeforeHIP30(t *testing.T) {
c := ChainConfig{
HIP30Epoch: big.NewInt(3),
}

require.True(t, c.IsOneEpochBeforeHIP30(big.NewInt(2)))
require.False(t, c.IsOneEpochBeforeHIP30(big.NewInt(3)))
}
7 changes: 4 additions & 3 deletions node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ func addPendingTransactions(registry *registry.Registry, newTxs types.Transactio
poolTxs = types.PoolTransactions{}
epoch = bc.CurrentHeader().Epoch()
acceptCx = bc.Config().AcceptsCrossTx(epoch)
isBeforeHIP30 = bc.Config().IsEpochBeforeHIP30(epoch)
isBeforeHIP30 = bc.Config().IsOneEpochBeforeHIP30(epoch)
nxtShards = shard.Schedule.InstanceForEpoch(new(big.Int).Add(epoch, common.Big1)).NumShards()
)
for _, tx := range newTxs {
Expand Down Expand Up @@ -497,7 +497,8 @@ func (node *Node) validateNodeMessage(ctx context.Context, payload []byte) (
if err := rlp.DecodeBytes(blocksPayload, &blocks); err != nil {
return nil, 0, errors.Wrap(err, "block decode error")
}
curBeaconHeight := node.Beaconchain().CurrentBlock().NumberU64()
curBeaconBlock := node.EpochChain().CurrentBlock()
curBeaconHeight := curBeaconBlock.NumberU64()
for _, block := range blocks {
// Ban blocks number that is smaller than tolerance
if block.NumberU64()+beaconBlockHeightTolerance <= curBeaconHeight {
Expand All @@ -507,7 +508,7 @@ func (node *Node) validateNodeMessage(ctx context.Context, payload []byte) (
} else if block.NumberU64()-beaconBlockHeightTolerance > curBeaconHeight {
utils.Logger().Debug().Uint64("receivedNum", block.NumberU64()).
Uint64("currentNum", curBeaconHeight).Msg("beacon block sync message rejected")
return nil, 0, errors.New("beacon block height too much higher than current height beyond tolerance")
return nil, 0, errors.Errorf("beacon block height too much higher than current height beyond tolerance, block %d, current %d, epoch %d , current %d", block.NumberU64(), curBeaconHeight, block.Epoch().Uint64(), curBeaconBlock.Epoch().Uint64())
} else if block.NumberU64() <= curBeaconHeight {
utils.Logger().Debug().Uint64("receivedNum", block.NumberU64()).
Uint64("currentNum", curBeaconHeight).Msg("beacon block sync message ignored")
Expand Down
6 changes: 3 additions & 3 deletions node/node_newblock.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ func (node *Node) ProposeNewBlock(commitSigs chan []byte) (*types.Block, error)
}
}

if !shard.Schedule.IsLastBlock(header.Number().Uint64()) {
if !shard.Schedule.IsLastBlock(header.Number().Uint64()) || node.Consensus.ShardID != 0 {
// Prepare normal and staking transactions retrieved from transaction pool
utils.AnalysisStart("proposeNewBlockChooseFromTxnPool")

Expand Down Expand Up @@ -255,7 +255,7 @@ func (node *Node) ProposeNewBlock(commitSigs chan []byte) (*types.Block, error)
len(crossLinksToPropose), len(allPending),
)
} else {
utils.Logger().Error().Err(err).Msgf(
utils.Logger().Warn().Err(err).Msgf(
"[ProposeNewBlock] Unable to Read PendingCrossLinks, number of crosslinks: %d",
len(allPending),
)
Expand Down Expand Up @@ -291,7 +291,7 @@ func (node *Node) ProposeNewBlock(commitSigs chan []byte) (*types.Block, error)
utils.Logger().Error().Err(err).Msg("[ProposeNewBlock] Failed finalizing the new block")
return nil, err
}
utils.Logger().Info().Msg("[ProposeNewBlock] verifying the new block header")
utils.Logger().Info().Msgf("[ProposeNewBlock] verifying the new block: shard %d, number %d, epoch %d", finalizedBlock.ShardID(), finalizedBlock.NumberU64(), finalizedBlock.Epoch().Uint64())
err = node.Blockchain().Validator().ValidateHeader(finalizedBlock, true)

if err != nil {
Expand Down
17 changes: 4 additions & 13 deletions node/worker/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,17 +160,18 @@ func (w *Worker) CommitTransactions(
// but the balance will immediately be returned to shard 1
cx, err := core.MayBalanceMigration(
w.current.gasPool,
w.beacon.CurrentHeader(),
w.GetCurrentHeader(),
w.current.state,
w.chain,
w.chain.Config(),
)
if err != nil {
if err == core.ErrNoMigrationPossible {
if errors.Is(err, core.ErrNoMigrationPossible) {
// means we do not accept transactions from the network
return nil
}
if err != core.ErrNoMigrationRequired {
if !errors.Is(err, core.ErrNoMigrationRequired) {

// this shard not migrating => ErrNoMigrationRequired
// any other error means exit this block
return err
Expand Down Expand Up @@ -413,16 +414,6 @@ func (w *Worker) GetCurrentReceipts() []*types.Receipt {
return w.current.receipts
}

// OutgoingReceipts get the receipts generated starting from the last state.
func (w *Worker) OutgoingReceipts() []*types.CXReceipt {
return w.current.outcxs
}

// IncomingReceipts get incoming receipts in destination shard that is received from source shard
func (w *Worker) IncomingReceipts() []*types.CXReceiptsProof {
return w.current.incxs
}

// CollectVerifiedSlashes sets w.current.slashes only to those that
// past verification
func (w *Worker) CollectVerifiedSlashes() error {
Expand Down
54 changes: 0 additions & 54 deletions rpc/preimages.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,57 +29,3 @@ func (s *PreimagesService) Export(_ context.Context, path string) error {
return core.ExportPreimages(s.hmy.BlockChain, path)
}

func (s *PreimagesService) Import(_ context.Context, path string) error {
// these are by default not blocking
return core.ImportPreimages(s.hmy.BlockChain, path)
}
func (s *PreimagesService) Generate(_ context.Context, start, end rpc.BlockNumber) error {
// earliestBlock: the number of blocks in the past where you can generate the preimage from the last block
earliestBlock := uint64(2)
currentBlockNum := s.hmy.CurrentBlock().NumberU64()

var startBlock uint64
switch start {
case rpc.EarliestBlockNumber:
startBlock = earliestBlock
case rpc.LatestBlockNumber:
startBlock = earliestBlock
case rpc.PendingBlockNumber:
startBlock = earliestBlock
default:
startBlock = uint64(start)
}

var endBlock = uint64(end)
switch end {
case rpc.EarliestBlockNumber:
endBlock = currentBlockNum
case rpc.LatestBlockNumber:
endBlock = currentBlockNum
case rpc.PendingBlockNumber:
endBlock = currentBlockNum
default:
endBlock = uint64(end)
}

fmt.Printf("Generating preimage from block %d to %d\n", startBlock, endBlock)

if number := currentBlockNum; number > endBlock {
fmt.Printf(
"Cropping generate endpoint from %d to %d\n",
endBlock, number,
)
endBlock = number
}

if startBlock >= endBlock {
fmt.Printf(
"Cropping generate startpoint from %d to %d\n",
startBlock, endBlock-earliestBlock,
)
startBlock = endBlock - earliestBlock
}

// these are by default not blocking
return core.GeneratePreimages(s.hmy.BlockChain, startBlock, endBlock)
}