Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
blukat29 committed Jul 5, 2024
1 parent 1aca7b1 commit a912b3f
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 1 deletion.
6 changes: 6 additions & 0 deletions blockchain/system/multicall.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,12 @@ func (caller *ContractCallerForMultiCall) CallContract(ctx context.Context, call
return result.Return(), err
}

// NewMultiCallContractCaller creates a new instance of ContractCaller for MultiCall contract.
func NewMultiCallContractCallerWithState(state *state.StateDB, chain backends.BlockChainForCaller, header *types.Header) (*multicall.MultiCallContractCaller, error) {
c := &ContractCallerForMultiCall{state, chain, header}
return multicall.NewMultiCallContractCaller(MultiCallAddr, c)
}

// NewMultiCallContractCaller creates a new instance of ContractCaller for MultiCall contract.
func NewMultiCallContractCaller(chain backends.BlockChainForCaller, header *types.Header) (*multicall.MultiCallContractCaller, error) {
state, err := chain.StateAt(header.Root)
Expand Down
3 changes: 3 additions & 0 deletions consensus/istanbul/backend/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -547,6 +547,9 @@ func (sb *backend) Finalize(chain consensus.ChainReader, header *types.Header, s
return nil, err
}

j, _ := json.Marshal(rewardSpec)
logger.Info("reward", "spec", string(j))

reward.DistributeBlockReward(state, rewardSpec.Rewards)

// RebalanceTreasury can modify the global state (state),
Expand Down
6 changes: 6 additions & 0 deletions node/cn/state_accessor.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
"github.com/kaiachain/kaia/blockchain/types"
"github.com/kaiachain/kaia/blockchain/vm"
"github.com/kaiachain/kaia/common"
"github.com/kaiachain/kaia/reward"
statedb2 "github.com/kaiachain/kaia/storage/statedb"
)

Expand Down Expand Up @@ -143,7 +144,12 @@ func (cn *CN) stateAtBlock(block *types.Block, reexec uint64, base *state.StateD
database.TrieDB().ReferenceRoot(root)
if !common.EmptyHash(parent) {
database.TrieDB().Dereference(parent)
if current.Header().Root != root {
logger.Warn("Historical state regeneration failed", "block", current.NumberU64(), "root", root, "expected", current.Header().Root)
}
}
a, err := reward.PrefetchStakingInfoFromMultiCall(current.NumberU64(), statedb)
logger.Warn("prefetched stakinginfo", "num", next+1, "a", a != nil, "err", err)
parent = root
}
if report {
Expand Down
46 changes: 45 additions & 1 deletion reward/staking_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ func GetStakingInfoForKaiaBlock(blockNum uint64) *StakingInfo {
return cachedStakingInfo
}

logger.Warn("staking cache miss", "blockNum", blockNum)
stakingInfo, err := updateKaiaStakingInfo(blockNum)
if err != nil {
logger.Error("failed to update kaia stakingInfo", "block number", blockNum, "err", err)
Expand Down Expand Up @@ -248,7 +249,7 @@ func updateKaiaStakingInfo(blockNum uint64) (*StakingInfo, error) {
}

addStakingInfoToCache(stakingInfo)
logger.Debug("Add a new stakingInfo to stakingInfoCache", "blockNum", blockNum)
logger.Warn("Add a new stakingInfo to stakingInfoCache", "blockNum", blockNum)

logger.Debug("Added stakingInfo", "stakingInfo", stakingInfo)
return stakingInfo, nil
Expand Down Expand Up @@ -316,6 +317,49 @@ func getStakingInfoFromMultiCall(blockNum uint64) (*StakingInfo, error) {
return newStakingInfo(stakingManager.blockchain, stakingManager.governanceHelper, blockNum, types, addrs, stakingAmounts...)
}

// NOTE: Even if the AddressBook contract code is erroneous and it returns unexpected result, this function should not return error in order not to stop block proposal.
// getStakingInfoFromMultiCall returns stakingInfo fetched from MultiCall contract.
// The MultiCall contract gets types and staking addresses from AddressBook contract, and balances of staking addresses.
func PrefetchStakingInfoFromMultiCall(blockNum uint64, _state *state.StateDB) (*StakingInfo, error) {
header := stakingManager.blockchain.GetHeaderByNumber(blockNum)
if header == nil {
return nil, fmt.Errorf("failed to get header by number %d", blockNum)
}

// Get staking info from multicall contract
caller, err := system.NewMultiCallContractCallerWithState(_state, stakingManager.blockchain, header)
if err != nil {
return nil, fmt.Errorf("failed to create multicall contract caller. root err: %s", err)
}

res, err := caller.MultiCallStakingInfo(&bind.CallOpts{BlockNumber: new(big.Int).SetUint64(blockNum)})
if err != nil {
return nil, fmt.Errorf("failed to call MultiCall contract. root err: %s", err)
}

types := res.TypeList
addrs := res.AddressList
stakingAmounts := res.StakingAmounts

if len(types) == 0 && len(addrs) == 0 {
// This is an expected behavior when the addressBook contract is not activated yet.
// Note that multicall contract calls address book internally.
logger.Info("The addressBook is not yet activated. Use empty stakingInfo")
return newEmptyStakingInfo(blockNum), nil
}

if len(types) != len(addrs) {
return nil, fmt.Errorf("length of type list and address list differ. len(type)=%d, len(addrs)=%d", len(types), len(addrs))
}

stakingInfo, err := newStakingInfo(stakingManager.blockchain, stakingManager.governanceHelper, blockNum, types, addrs, stakingAmounts...)
if err == nil {
logger.Warn("prefetched stakinginfo", "num", blockNum)
addStakingInfoToCache(stakingInfo)
}
return stakingInfo, err
}

// NOTE: Even if the AddressBook contract code is erroneous and it returns unexpected result, this function should not return error in order not to stop block proposal.
// getStakingInfoFromAddressBook returns stakingInfo fetched from AddressBook contract
// 1. If calling AddressBook contract fails, it returns error
Expand Down

0 comments on commit a912b3f

Please sign in to comment.