Skip to content

Commit

Permalink
Merge pull request #168 from dogechain-lab/hotfix/hardfork-on-bridging
Browse files Browse the repository at this point in the history
[HardFork] portland hardfork on fixing bridge withdrawn issue
  • Loading branch information
abrahamcruise321 authored Sep 13, 2022
2 parents 164a206 + f7a86e1 commit 1cd7381
Show file tree
Hide file tree
Showing 10 changed files with 304 additions and 2 deletions.
11 changes: 10 additions & 1 deletion blockchain/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ import (
"github.com/dogechain-lab/dogechain/blockchain/storage"
"github.com/dogechain-lab/dogechain/blockchain/storage/memory"
"github.com/dogechain-lab/dogechain/chain"
"github.com/dogechain-lab/dogechain/contracts/upgrader"
"github.com/dogechain-lab/dogechain/helper/common"
"github.com/dogechain-lab/dogechain/state"
"github.com/dogechain-lab/dogechain/types"
"github.com/dogechain-lab/dogechain/types/buildroot"

"github.com/hashicorp/go-hclog"
lru "github.com/hashicorp/golang-lru"
)
Expand Down Expand Up @@ -853,6 +853,15 @@ func (b *Blockchain) executeBlockTransactions(block *types.Block) (*BlockResult,
return nil, err
}

// upgrade system if needed
upgrader.UpgradeSystem(
b.Config().ChainID,
b.Config().Forks,
block.Number(),
txn.Txn(),
b.logger,
)

_, root := txn.Commit()

// Append the receipts to the receipts cache
Expand Down
27 changes: 26 additions & 1 deletion chain/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ type Params struct {
ChainID int `json:"chainID"`
Engine map[string]interface{} `json:"engine"`
BlockGasTarget uint64 `json:"blockGasTarget"`
BlackList []string `json:"blackList,omitempty"`
}

func (p *Params) GetEngine() string {
Expand All @@ -31,6 +32,15 @@ type Forks struct {
EIP150 *Fork `json:"EIP150,omitempty"`
EIP158 *Fork `json:"EIP158,omitempty"`
EIP155 *Fork `json:"EIP155,omitempty"`
Portland *Fork `json:"portland,omitempty"`
}

func (f *Forks) on(ff *Fork, block uint64) bool {
if ff == nil {
return false
}

return ff.On(block)
}

func (f *Forks) active(ff *Fork, block uint64) bool {
Expand Down Expand Up @@ -69,6 +79,10 @@ func (f *Forks) IsEIP155(block uint64) bool {
return f.active(f.EIP155, block)
}

func (f *Forks) IsPortland(block uint64) bool {
return f.active(f.Portland, block)
}

func (f *Forks) At(block uint64) ForksInTime {
return ForksInTime{
Homestead: f.active(f.Homestead, block),
Expand All @@ -79,9 +93,14 @@ func (f *Forks) At(block uint64) ForksInTime {
EIP150: f.active(f.EIP150, block),
EIP158: f.active(f.EIP158, block),
EIP155: f.active(f.EIP155, block),
Portland: f.active(f.Portland, block),
}
}

func (f *Forks) IsOnPortland(block uint64) bool {
return f.on(f.Portland, block)
}

type Fork uint64

func NewFork(n uint64) *Fork {
Expand All @@ -90,6 +109,10 @@ func NewFork(n uint64) *Fork {
return &f
}

func (f Fork) On(block uint64) bool {
return block == uint64(f)
}

func (f Fork) Active(block uint64) bool {
return block >= uint64(f)
}
Expand All @@ -106,7 +129,8 @@ type ForksInTime struct {
Istanbul,
EIP150,
EIP158,
EIP155 bool
EIP155,
Portland bool
}

var AllForksEnabled = &Forks{
Expand All @@ -118,4 +142,5 @@ var AllForksEnabled = &Forks{
Constantinople: NewFork(0),
Petersburg: NewFork(0),
Istanbul: NewFork(0),
Portland: NewFork(10222),
}
10 changes: 10 additions & 0 deletions consensus/dev/dev.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/dogechain-lab/dogechain/blockchain"
"github.com/dogechain-lab/dogechain/consensus"
"github.com/dogechain-lab/dogechain/contracts/upgrader"
"github.com/dogechain-lab/dogechain/helper/progress"
"github.com/dogechain-lab/dogechain/state"
"github.com/dogechain-lab/dogechain/txpool"
Expand Down Expand Up @@ -185,6 +186,15 @@ func (d *Dev) writeNewBlock(parent *types.Header) error {

txns := d.writeTransactions(gasLimit, transition)

// upgrade system if needed
upgrader.UpgradeSystem(
d.blockchain.Config().ChainID,
d.blockchain.Config().Forks,
header.Number,
transition.Txn(),
d.logger,
)

// Commit the changes
_, root := transition.Commit()

Expand Down
10 changes: 10 additions & 0 deletions consensus/ibft/ibft.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

"github.com/dogechain-lab/dogechain/consensus"
"github.com/dogechain-lab/dogechain/consensus/ibft/proto"
"github.com/dogechain-lab/dogechain/contracts/upgrader"
"github.com/dogechain-lab/dogechain/crypto"
"github.com/dogechain-lab/dogechain/helper/common"
"github.com/dogechain-lab/dogechain/helper/hex"
Expand Down Expand Up @@ -626,6 +627,15 @@ func (i *Ibft) buildBlock(snap *Snapshot, parent *types.Header) (*types.Block, e
return nil, err
}

// upgrade system if needed
upgrader.UpgradeSystem(
i.config.Params.ChainID,
i.config.Params.Forks,
header.Number,
transition.Txn(),
i.logger,
)

_, root := transition.Commit()
header.StateRoot = root
header.GasUsed = transition.TotalGas()
Expand Down
70 changes: 70 additions & 0 deletions contracts/abis/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,26 @@ const BridgeJSONABI = `[
"stateMutability": "nonpayable",
"type": "constructor"
},
{
"anonymous": false,
"inputs":
[
{
"indexed": true,
"internalType": "address",
"name": "sender",
"type": "address"
},
{
"indexed": true,
"internalType": "uint256",
"name": "amount",
"type": "uint256"
}
],
"name": "Burned",
"type": "event"
},
{
"anonymous": false,
"inputs":
Expand Down Expand Up @@ -646,6 +666,21 @@ const BridgeJSONABI = `[
"name": "Withdrawn",
"type": "event"
},
{
"inputs":
[
{
"internalType": "uint256",
"name": "amount",
"type": "uint256"
}
],
"name": "addBalance",
"outputs":
[],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs":
[
Expand All @@ -661,6 +696,26 @@ const BridgeJSONABI = `[
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs":
[
{
"internalType": "address",
"name": "account",
"type": "address"
},
{
"internalType": "uint256",
"name": "amount",
"type": "uint256"
}
],
"name": "burn",
"outputs":
[],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs":
[
Expand Down Expand Up @@ -862,6 +917,21 @@ const BridgeJSONABI = `[
"stateMutability": "view",
"type": "function"
},
{
"inputs":
[
{
"internalType": "uint256",
"name": "amount",
"type": "uint256"
}
],
"name": "subBalance",
"outputs":
[],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs":
[],
Expand Down
45 changes: 45 additions & 0 deletions contracts/bridge/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@ import (
const (
EventDeposited = "Deposited"
EventWithdrawn = "Withdrawn"
EventBurned = "Burned"

fieldReceiver = "receiver"
fieldAmount = "amount"
fieldFee = "fee"
fieldSender = "sender"
)

// Frequently used methods. Must exist.
Expand All @@ -25,6 +27,8 @@ var (
BridgeDepositedEventID = types.Hash(BridgeDepositedEvent.ID())
BridgeWithdrawnEvent = abis.BridgeABI.Events[EventWithdrawn]
BridgeWithdrawnEventID = types.Hash(BridgeWithdrawnEvent.ID())
BridgeEventBurnedEvent = abis.BridgeABI.Events[EventBurned]
BridgeBurnedEventID = types.Hash(BridgeEventBurnedEvent.ID())
)

type DepositedLog struct {
Expand Down Expand Up @@ -125,3 +129,44 @@ func getBigIntFromWithdrawnLog(log map[string]interface{}, key string) (*big.Int

return bigVal, nil
}

type BurnedLog struct {
Sender types.Address
Amount *big.Int
}

func ParseBridgeBurnedLog(log *types.Log) (*BurnedLog, error) {
topics := make([]web3.Hash, 0, len(log.Topics))
for _, topic := range log.Topics {
topics = append(topics, web3.Hash(topic))
}

w3Log, err := BridgeEventBurnedEvent.ParseLog(&web3.Log{
Address: web3.Address(log.Address),
Topics: topics,
Data: log.Data,
})
if err != nil {
return nil, err
}

sender, ok := w3Log[fieldSender]
if !ok {
return nil, errors.New("address not exists in Burned event")
}

account, ok := sender.(web3.Address)
if !ok {
return nil, errors.New("address downcast failed")
}

amount, err := getBigIntFromWithdrawnLog(w3Log, fieldAmount)
if err != nil {
return nil, err
}

return &BurnedLog{
Sender: types.Address(account),
Amount: amount,
}, nil
}
101 changes: 101 additions & 0 deletions contracts/upgrader/upgrader.go

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,12 @@ func NewServer(config *Config) (*Server, error) {
state: m.state,
Blockchain: m.blockchain,
}

blackList := make([]types.Address, len(m.config.Chain.Params.BlackList))
for i, a := range m.config.Chain.Params.BlackList {
blackList[i] = types.StringToAddress(a)
}

// start transaction pool
m.txpool, err = txpool.NewTxPool(
logger,
Expand All @@ -245,6 +251,7 @@ func NewServer(config *Config) (*Server, error) {
MaxAccountDemotions: m.config.MaxAccountDemotions,
PruneTickSeconds: m.config.PruneTickSeconds,
PromoteOutdateSeconds: m.config.PromoteOutdateSeconds,
BlackList: blackList,
},
)
if err != nil {
Expand Down
10 changes: 10 additions & 0 deletions state/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,16 @@ func (t *Transition) handleBridgeLogs(msg *types.Transaction, logs []*types.Log)

// the fee goes to system Vault contract
t.state.AddBalance(systemcontracts.AddrVaultContract, parsedLog.Fee)
case bridge.BridgeBurnedEventID:
parsedLog, err := bridge.ParseBridgeBurnedLog(log)
if err != nil {
return err
}

// burn
if err := t.state.SubBalance(parsedLog.Sender, parsedLog.Amount); err != nil {
return err
}
}
}

Expand Down
Loading

0 comments on commit 1cd7381

Please sign in to comment.