Skip to content

Commit

Permalink
Merge pull request #21 from blukat29/polish-202406
Browse files Browse the repository at this point in the history
Tidy RebalanceTreasury and SimulatedBackend
  • Loading branch information
blukat29 authored Jun 22, 2024
2 parents c03bca6 + 2cf1248 commit 788a8be
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 13 deletions.
7 changes: 6 additions & 1 deletion accounts/abi/bind/backends/simulated.go
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,12 @@ func (b *SimulatedBackend) PendingNonceAt(_ context.Context, account common.Addr
// SuggestGasPrice implements ContractTransactor.SuggestGasPrice. Since the simulated
// chain doesn't have miners, we just return a gas price of 1 for any call.
func (b *SimulatedBackend) SuggestGasPrice(_ context.Context) (*big.Int, error) {
return new(big.Int).SetUint64(b.config.UnitPrice), nil
current := b.blockchain.CurrentBlock()
if b.blockchain.Config().IsMagmaForkEnabled(current.Number()) {
return new(big.Int).Mul(current.Header().BaseFee, big.NewInt(2)), nil
} else {
return new(big.Int).SetUint64(b.config.UnitPrice), nil
}
}

// EstimateGas executes the requested code against the latest block/state and
Expand Down
4 changes: 0 additions & 4 deletions blockchain/state_transition.go
Original file line number Diff line number Diff line change
Expand Up @@ -487,7 +487,3 @@ func (st *StateTransition) refundGas(refundQuotient uint64) {
func (st *StateTransition) gasUsed() uint64 {
return st.initialGas - st.gas
}

func getBurnAmountMagma(fee *big.Int) *big.Int {
return new(big.Int).Div(fee, big.NewInt(2))
}
5 changes: 1 addition & 4 deletions blockchain/system/rebalance.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ func newRebalanceReceipt() *rebalanceResult {
}
}

func (result *rebalanceResult) memo(isKip103 bool) []byte {
func (result *rebalanceResult) Memo(isKip103 bool) []byte {
var (
memo []byte
err error
Expand Down Expand Up @@ -322,8 +322,5 @@ func RebalanceTreasury(state *state.StateDB, chain backends.BlockChainForCaller,
remainder := new(big.Int).Sub(totalZeroedAmount, totalAllocatedAmount)
result.Burnt.Add(result.Burnt, remainder)
result.Success = true

// Leave a memo for logging
logger.Info("successfully executed treasury rebalancing", "memo", string(result.memo(isKIP103)))
return result, nil
}
6 changes: 3 additions & 3 deletions blockchain/system/rebalance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ func rebalanceTreasury(t *testing.T, sender *bind.TransactOpts, config *params.C
res, err := RebalanceTreasury(state, chain, chain.CurrentHeader())
if chain.Config().Kip103CompatibleBlock != nil && tc.expectBurnt.Cmp(big.NewInt(0)) == -1 {
assert.Equal(t, ErrRebalanceNotEnoughBalance, err)
t.Log(string(res.memo(true)))
t.Log(string(res.Memo(true)))
continue
}

Expand Down Expand Up @@ -250,9 +250,9 @@ func rebalanceTreasury(t *testing.T, sender *bind.TransactOpts, config *params.C
// assert.Equal(t, tc.expectKip103Memo, string(res.memo(isKip103)))
//}

t.Log(string(res.memo(isKip103)))
t.Log(string(res.Memo(isKip103)))
if !isKip103 {
assert.Equal(t, tc.expectKip160Memo, string(res.memo(isKip103)))
assert.Equal(t, tc.expectKip160Memo, string(res.Memo(isKip103)))
}
}
}
6 changes: 5 additions & 1 deletion consensus/istanbul/backend/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -553,9 +553,13 @@ func (sb *backend) Finalize(chain consensus.ChainReader, header *types.Header, s
// so the existing state db should be used to apply the rebalancing result.
// Only on the KIP-103 or KIP-160 hardfork block, the following logic should be executed
if chain.Config().IsKIP160ForkBlock(header.Number) || chain.Config().IsKIP103ForkBlock(header.Number) {
_, err := system.RebalanceTreasury(state, chain, header)
rebalanceResult, err := system.RebalanceTreasury(state, chain, header)
if err != nil {
logger.Error("failed to execute treasury rebalancing. State not changed", "err", err)
} else {
// Leave the memo in the log for later contract finalization
isKIP103 := chain.Config().IsKIP103ForkBlock(header.Number) // because memo format differs between KIP-103 and KIP-160
logger.Info("successfully executed treasury rebalancing", "memo", string(rebalanceResult.Memo(isKIP103)))
}
}

Expand Down

0 comments on commit 788a8be

Please sign in to comment.