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

Qip10 fix and new genallocs #2227

Merged
merged 2 commits into from
Oct 15, 2024
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
22 changes: 22 additions & 0 deletions common/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -766,3 +766,25 @@ func NewChainsAdded(expansionNumber uint8) []Location {
}
return newChains
}

// SetBlockHashForQuai sets the correct first 4 bytes in the block hash for QIP10 and Quai origin
func SetBlockHashForQuai(blockHash Hash, nodeLocation Location) Hash {
// Set the first byte of the block hash to the zone prefix
origin := (uint8(nodeLocation[0]) * 16) + uint8(nodeLocation[1])
blockHash[0] = origin
blockHash[2] = origin
blockHash[1] &= 0x7F // 01111111 in binary (set first bit to 0)
blockHash[3] &= 0x7F // 01111111 in binary (set first bit to 0)
return blockHash
}

// SetBlockHashForQuai sets the correct first 4 bytes in the block hash for QIP10 and Qi origin
func SetBlockHashForQi(blockHash Hash, nodeLocation Location) Hash {
// Set the first byte of the block hash to the zone prefix
origin := (uint8(nodeLocation[0]) * 16) + uint8(nodeLocation[1])
blockHash[0] = origin
blockHash[2] = origin
blockHash[1] |= 0x80 // 10000000 in binary (set first bit to 1)
blockHash[3] |= 0x80 // 10000000 in binary (set first bit to 1)
return blockHash
}
22 changes: 13 additions & 9 deletions core/state_processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,7 @@ func (p *StateProcessor) Process(block *types.WorkObject, batch ethdb.Batch) (ty
nodeLocation = p.hc.NodeLocation()
nodeCtx = p.hc.NodeCtx()
blockNumber = block.Number(nodeCtx)
parentHash = block.ParentHash(nodeCtx)
allLogs []*types.Log
gp = new(types.GasPool).AddGas(block.GasLimit())
)
Expand Down Expand Up @@ -674,36 +675,39 @@ func (p *StateProcessor) Process(block *types.WorkObject, batch ethdb.Batch) (ty
primaryCoinbase := block.PrimaryCoinbase()
secondaryCoinbase := block.SecondaryCoinbase()

// parent hash encoding for populating into the originating tx hash for coinbase
origin := block.ParentHash(nodeCtx)
origin[0] = byte(block.Location().Region())
origin[1] = byte(block.Location().Zone())
// If the primary coinbase belongs to a ledger and there is no fees
// for other ledger, there is no etxs emitted for the other ledger
if bytes.Equal(block.PrimaryCoinbase().Bytes(), quaiCoinbase.Bytes()) {
coinbaseReward := misc.CalculateReward(parent, block.WorkObjectHeader())
blockReward := new(big.Int).Add(coinbaseReward, quaiFees)
coinbaseEtx := types.NewTx(&types.ExternalTx{To: &primaryCoinbase, Gas: params.TxGas, Value: blockReward, EtxType: types.CoinbaseType, OriginatingTxHash: origin, ETXIndex: uint16(len(emittedEtxs)), Sender: primaryCoinbase, Data: []byte{block.Lock()}})

coinbaseEtx := types.NewTx(&types.ExternalTx{To: &primaryCoinbase, Gas: params.TxGas, Value: blockReward, EtxType: types.CoinbaseType, OriginatingTxHash: common.SetBlockHashForQuai(parentHash, nodeLocation), ETXIndex: uint16(len(emittedEtxs)), Sender: primaryCoinbase, Data: []byte{block.Lock()}})
emittedEtxs = append(emittedEtxs, coinbaseEtx)
if qiFees.Cmp(big.NewInt(0)) != 0 {
coinbaseEtx := types.NewTx(&types.ExternalTx{To: &secondaryCoinbase, Gas: params.TxGas, Value: qiFees, EtxType: types.CoinbaseType, OriginatingTxHash: origin, ETXIndex: uint16(len(emittedEtxs)), Sender: secondaryCoinbase, Data: []byte{block.Lock()}})
coinbaseEtx := types.NewTx(&types.ExternalTx{To: &secondaryCoinbase, Gas: params.TxGas, Value: qiFees, EtxType: types.CoinbaseType, OriginatingTxHash: common.SetBlockHashForQi(parentHash, nodeLocation), ETXIndex: uint16(len(emittedEtxs)), Sender: secondaryCoinbase, Data: []byte{block.Lock()}})
emittedEtxs = append(emittedEtxs, coinbaseEtx)
}
} else if bytes.Equal(block.PrimaryCoinbase().Bytes(), qiCoinbase.Bytes()) {
coinbaseReward := misc.CalculateReward(parent, block.WorkObjectHeader())
blockReward := new(big.Int).Add(coinbaseReward, qiFees)
coinbaseEtx := types.NewTx(&types.ExternalTx{To: &primaryCoinbase, Gas: params.TxGas, Value: blockReward, EtxType: types.CoinbaseType, OriginatingTxHash: origin, ETXIndex: uint16(len(emittedEtxs)), Sender: primaryCoinbase, Data: []byte{block.Lock()}})
coinbaseEtx := types.NewTx(&types.ExternalTx{To: &primaryCoinbase, Gas: params.TxGas, Value: blockReward, EtxType: types.CoinbaseType, OriginatingTxHash: common.SetBlockHashForQi(parentHash, nodeLocation), ETXIndex: uint16(len(emittedEtxs)), Sender: primaryCoinbase, Data: []byte{block.Lock()}})
emittedEtxs = append(emittedEtxs, coinbaseEtx)
if quaiFees.Cmp(big.NewInt(0)) != 0 {
coinbaseEtx := types.NewTx(&types.ExternalTx{To: &secondaryCoinbase, Gas: params.TxGas, Value: quaiFees, EtxType: types.CoinbaseType, OriginatingTxHash: origin, ETXIndex: uint16(len(emittedEtxs)), Sender: secondaryCoinbase, Data: []byte{block.Lock()}})
coinbaseEtx := types.NewTx(&types.ExternalTx{To: &secondaryCoinbase, Gas: params.TxGas, Value: quaiFees, EtxType: types.CoinbaseType, OriginatingTxHash: common.SetBlockHashForQuai(parentHash, nodeLocation), ETXIndex: uint16(len(emittedEtxs)), Sender: secondaryCoinbase, Data: []byte{block.Lock()}})
emittedEtxs = append(emittedEtxs, coinbaseEtx)
}
}
// Add an etx for each workshare for it to be rewarded
for _, uncle := range block.Uncles() {
reward := misc.CalculateReward(parent, uncle)
uncleCoinbase := uncle.PrimaryCoinbase()
emittedEtxs = append(emittedEtxs, types.NewTx(&types.ExternalTx{To: &uncleCoinbase, Gas: params.TxGas, Value: reward, EtxType: types.CoinbaseType, OriginatingTxHash: origin, ETXIndex: uint16(len(emittedEtxs)), Sender: uncleCoinbase, Data: []byte{uncle.Lock()}}))
var originHash common.Hash
if uncleCoinbase.IsInQuaiLedgerScope() {
originHash = common.SetBlockHashForQuai(parentHash, nodeLocation)
} else {
originHash = common.SetBlockHashForQi(parentHash, nodeLocation)
}
emittedEtxs = append(emittedEtxs, types.NewTx(&types.ExternalTx{To: &uncleCoinbase, Gas: params.TxGas, Value: reward, EtxType: types.CoinbaseType, OriginatingTxHash: originHash, ETXIndex: uint16(len(emittedEtxs)), Sender: uncleCoinbase, Data: []byte{uncle.Lock()}}))
}

updatedTokenChoiceSet, err := CalculateTokenChoicesSet(p.hc, parent, emittedEtxs)
Expand Down
2 changes: 1 addition & 1 deletion core/types/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -710,7 +710,7 @@ func (tx *Transaction) Hash(location ...byte) (h common.Hash) {
}
case QiTxType:
// the origin of this tx is the *destination* of the utxos being spent
origin := tx.TxIn()[0].PreviousOutPoint.TxHash[1]
origin := tx.TxIn()[0].PreviousOutPoint.TxHash[2]
h[0] = origin
h[1] |= 0x80 // 10000000 in binary (set first bit to 1)
h[2] = origin
Expand Down
4 changes: 2 additions & 2 deletions core/types/transaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -789,8 +789,8 @@ func qiTxData() (*Transaction, common.Hash) {

func TestQiTxHash(t *testing.T) {
_, hash := qiTxData()
correctHash := common.HexToHash("0x20b720e4860b3db006d7a19fed6be3efe5619f53f499ef561f42c46bc12b555d")
require.Equal(t, hash, correctHash, "Hash not equal to expected hash")
correctHash := common.HexToHash("0x3ab73ae4860b3db006d7a19fed6be3efe5619f53f499ef561f42c46bc12b555d")
require.Equal(t, correctHash, hash, "Hash not equal to expected hash")
}

func FuzzQiTxHashingChainID(f *testing.F) {
Expand Down
21 changes: 11 additions & 10 deletions core/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -599,33 +599,28 @@ func (w *worker) GeneratePendingHeader(block *types.WorkObject, fill bool, txs t
return nil, err
}

// Encode the parent hash with the correct origin location and use it in the OriginatingTxHash field for coinbase
origin := block.Hash()
origin[0] = byte(w.hc.NodeLocation().Region())
origin[1] = byte(w.hc.NodeLocation().Zone())

// If the primary coinbase belongs to a ledger and there is no fees
// for other ledger, there is no etxs emitted for the other ledger
if bytes.Equal(work.wo.PrimaryCoinbase().Bytes(), quaiCoinbase.Bytes()) {
coinbaseReward := misc.CalculateReward(block, work.wo.WorkObjectHeader())
blockReward := new(big.Int).Add(coinbaseReward, work.quaiFees)
primaryCoinbase := w.GetPrimaryCoinbase()
coinbaseEtx := types.NewTx(&types.ExternalTx{To: &primaryCoinbase, Gas: params.TxGas, Value: blockReward, EtxType: types.CoinbaseType, OriginatingTxHash: origin, ETXIndex: uint16(len(work.etxs)), Sender: primaryCoinbase, Data: []byte{defaultCoinbaseLockup}})
coinbaseEtx := types.NewTx(&types.ExternalTx{To: &primaryCoinbase, Gas: params.TxGas, Value: blockReward, EtxType: types.CoinbaseType, OriginatingTxHash: common.SetBlockHashForQuai(block.Hash(), w.hc.NodeLocation()), ETXIndex: uint16(len(work.etxs)), Sender: primaryCoinbase, Data: []byte{defaultCoinbaseLockup}})
work.etxs = append(work.etxs, coinbaseEtx)
if work.utxoFees.Cmp(big.NewInt(0)) != 0 {
secondaryCoinbase := w.GetSecondaryCoinbase()
coinbaseEtx := types.NewTx(&types.ExternalTx{To: &secondaryCoinbase, Gas: params.TxGas, Value: work.utxoFees, EtxType: types.CoinbaseType, OriginatingTxHash: origin, ETXIndex: uint16(len(work.etxs)), Sender: w.secondaryCoinbase, Data: []byte{defaultCoinbaseLockup}})
coinbaseEtx := types.NewTx(&types.ExternalTx{To: &secondaryCoinbase, Gas: params.TxGas, Value: work.utxoFees, EtxType: types.CoinbaseType, OriginatingTxHash: common.SetBlockHashForQi(block.Hash(), w.hc.NodeLocation()), ETXIndex: uint16(len(work.etxs)), Sender: w.secondaryCoinbase, Data: []byte{defaultCoinbaseLockup}})
work.etxs = append(work.etxs, coinbaseEtx)
}
} else if bytes.Equal(work.wo.PrimaryCoinbase().Bytes(), qiCoinbase.Bytes()) {
coinbaseReward := misc.CalculateReward(block, work.wo.WorkObjectHeader())
blockReward := new(big.Int).Add(coinbaseReward, work.utxoFees)
primaryCoinbase := w.GetPrimaryCoinbase()
coinbaseEtx := types.NewTx(&types.ExternalTx{To: &primaryCoinbase, Gas: params.TxGas, Value: blockReward, EtxType: types.CoinbaseType, OriginatingTxHash: origin, ETXIndex: uint16(len(work.etxs)), Sender: primaryCoinbase, Data: []byte{defaultCoinbaseLockup}})
coinbaseEtx := types.NewTx(&types.ExternalTx{To: &primaryCoinbase, Gas: params.TxGas, Value: blockReward, EtxType: types.CoinbaseType, OriginatingTxHash: common.SetBlockHashForQi(block.Hash(), w.hc.NodeLocation()), ETXIndex: uint16(len(work.etxs)), Sender: primaryCoinbase, Data: []byte{defaultCoinbaseLockup}})
work.etxs = append(work.etxs, coinbaseEtx)
if work.quaiFees.Cmp(big.NewInt(0)) != 0 {
secondaryCoinbase := w.GetSecondaryCoinbase()
coinbaseEtx := types.NewTx(&types.ExternalTx{To: &secondaryCoinbase, Gas: params.TxGas, Value: work.quaiFees, EtxType: types.CoinbaseType, OriginatingTxHash: origin, ETXIndex: uint16(len(work.etxs)), Sender: secondaryCoinbase, Data: []byte{defaultCoinbaseLockup}})
coinbaseEtx := types.NewTx(&types.ExternalTx{To: &secondaryCoinbase, Gas: params.TxGas, Value: work.quaiFees, EtxType: types.CoinbaseType, OriginatingTxHash: common.SetBlockHashForQuai(block.Hash(), w.hc.NodeLocation()), ETXIndex: uint16(len(work.etxs)), Sender: secondaryCoinbase, Data: []byte{defaultCoinbaseLockup}})
work.etxs = append(work.etxs, coinbaseEtx)
}
}
Expand All @@ -634,7 +629,13 @@ func (w *worker) GeneratePendingHeader(block *types.WorkObject, fill bool, txs t
for _, uncle := range uncles {
reward := misc.CalculateReward(block, uncle)
uncleCoinbase := uncle.PrimaryCoinbase()
work.etxs = append(work.etxs, types.NewTx(&types.ExternalTx{To: &uncleCoinbase, Gas: params.TxGas, Value: reward, EtxType: types.CoinbaseType, OriginatingTxHash: origin, ETXIndex: uint16(len(work.etxs)), Sender: uncleCoinbase, Data: []byte{uncle.Lock()}}))
var originHash common.Hash
if uncleCoinbase.IsInQuaiLedgerScope() {
originHash = common.SetBlockHashForQuai(block.Hash(), w.hc.NodeLocation())
} else {
originHash = common.SetBlockHashForQi(block.Hash(), w.hc.NodeLocation())
}
work.etxs = append(work.etxs, types.NewTx(&types.ExternalTx{To: &uncleCoinbase, Gas: params.TxGas, Value: reward, EtxType: types.CoinbaseType, OriginatingTxHash: originHash, ETXIndex: uint16(len(work.etxs)), Sender: uncleCoinbase, Data: []byte{uncle.Lock()}}))
}

}
Expand Down
2 changes: 1 addition & 1 deletion genallocs/gen_alloc_qi_cyprus1.json
Djadih marked this conversation as resolved.
Show resolved Hide resolved

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion genallocs/gen_alloc_qi_cyprus2.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion genallocs/gen_alloc_qi_cyprus3.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion genallocs/gen_alloc_qi_hydra1.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion genallocs/gen_alloc_qi_hydra2.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion genallocs/gen_alloc_qi_hydra3.json

Large diffs are not rendered by default.

Loading
Loading