Skip to content

kaiax/auction: implement bid bundling #299

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

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
10 changes: 9 additions & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,14 @@ executors:
auth:
username: $DOCKER_LOGIN
password: $DOCKER_PASSWORD
test-tests-executor: # this executor is for test-tests job
working_directory: ~/go/src/github.com/kaiachain/kaia
resource_class: medium+
docker:
- image: kaiachain/build_base:go1.23-solc0.8.13-ubuntu-22.04
auth:
username: $DOCKER_LOGIN
password: $DOCKER_PASSWORD
test-others-executor: # this executor is for test-others job
working_directory: /go/src/github.com/kaiachain/kaia
resource_class: xlarge
Expand Down Expand Up @@ -396,7 +404,7 @@ jobs:
no_output_timeout: 30m
command: make test-node
test-tests:
executor: test-executor
executor: test-tests-executor
steps:
- checkout
- run:
Expand Down
28 changes: 28 additions & 0 deletions blockchain/system/auction.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"github.com/kaiachain/kaia/accounts/abi/bind"
"github.com/kaiachain/kaia/common"
contracts "github.com/kaiachain/kaia/contracts/contracts/system_contracts/auction"
"github.com/kaiachain/kaia/kaiax/auction"
)

func ReadAuctioneer(backend bind.ContractCaller, contractAddr common.Address, num *big.Int) (common.Address, error) {
Expand All @@ -33,3 +34,30 @@ func ReadAuctioneer(backend bind.ContractCaller, contractAddr common.Address, nu
opts := &bind.CallOpts{BlockNumber: num}
return caller.Auctioneer(opts)
}

func EncodeAuctionCallData(bid *auction.Bid) ([]byte, error) {
abi, err := contracts.IAuctionEntryPointMetaData.GetAbi()
if err != nil {
return nil, err
}

input := contracts.IAuctionEntryPointAuctionTx{
TargetTxHash: bid.TargetTxHash,
BlockNumber: big.NewInt(int64(bid.BlockNumber)),
Sender: bid.Sender,
To: bid.To,
Nonce: big.NewInt(int64(bid.Nonce)),
Bid: bid.Bid,
CallGasLimit: big.NewInt(int64(bid.CallGasLimit)),
Data: bid.Data,
SearcherSig: bid.SearcherSig,
AuctioneerSig: bid.AuctioneerSig,
}

data, err := abi.Pack("call", input)
if err != nil {
return nil, err
}

return data, nil
}
95 changes: 95 additions & 0 deletions blockchain/system/auction_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
// Copyright 2025 The Kaia Authors
// This file is part of the Kaia library.
//
// The Kaia library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The Kaia library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the Kaia library. If not, see <http://www.gnu.org/licenses/>.

package system

import (
"math/big"
"testing"

"github.com/kaiachain/kaia/accounts/abi/bind"
"github.com/kaiachain/kaia/accounts/abi/bind/backends"
"github.com/kaiachain/kaia/blockchain"
"github.com/kaiachain/kaia/common"
contracts "github.com/kaiachain/kaia/contracts/contracts/testing/system_contracts"
"github.com/kaiachain/kaia/crypto"
"github.com/kaiachain/kaia/kaiax/auction"
"github.com/kaiachain/kaia/log"
"github.com/kaiachain/kaia/params"
"github.com/stretchr/testify/assert"
)

func TestReadAuctioneer(t *testing.T) {
log.EnableLogForTest(log.LvlCrit, log.LvlWarn)
var (
senderKey, _ = crypto.GenerateKey()
sender = bind.NewKeyedTransactor(senderKey)
addr = common.HexToAddress("0xaaaa")

alloc = blockchain.GenesisAlloc{
sender.From: {
Balance: big.NewInt(params.KAIA),
},
RegistryAddr: {
Code: RegistryMockCode,
Balance: common.Big0,
},
AuctionEntryPointAddrMock: {
Code: AuctionEntryPointMockCode,
Balance: common.Big0,
Storage: map[common.Hash]common.Hash{
common.BytesToHash([]byte{0x00}): addr.Hash(),
},
},
}

backend = backends.NewSimulatedBackend(alloc)
)

contract, _ := contracts.NewRegistryMockTransactor(RegistryAddr, backend)
contract.Register(sender, AuctionEntryPointName, AuctionEntryPointAddrMock, common.Big1)
backend.Commit()

auctioneer, err := ReadAuctioneer(backend, AuctionEntryPointAddrMock, common.Big1)
assert.Nil(t, err)
assert.Equal(t, addr, auctioneer)
}

var data = auction.BidData{
TargetTxHash: common.HexToHash("0xcf5879724726228474db71caf83955a61341f2e5bacaa2b9d37f6e5f48241cbc"),
BlockNumber: 11,
Sender: common.HexToAddress("0x70997970C51812dc3A010C7d01b50e0d17dc79C8"),
To: common.HexToAddress("0x5FC8d32690cc91D4c39d9d3abcBD16989F875707"),
Nonce: 0,
Bid: new(big.Int).SetBytes(common.Hex2Bytes("8ac7230489e80000")),
CallGasLimit: 10000000,
Data: common.Hex2Bytes("d09de08a"),
SearcherSig: common.Hex2Bytes("45eb1182b8ccffc953ebbb192dd5376cf3a9f53c060a3de6e42526ebe0cb91682e571067ac5aa38d5742f6534d0adc95793443056bd2041d551c7f3aff29967e1c"),
AuctioneerSig: common.Hex2Bytes("27bba77731bbba25dcde852c54d05eb8d095b2d71b05ff9611ef5a123d1b2af01a7b42d0c8f8ab5ba07456932f4b1d29ff5f6e7b7fb8f59d41d22c4294d23bd91c"),
}

var testBid = &auction.Bid{
BidData: data,
}

func TestEncodeAuctionCallData(t *testing.T) {
log.EnableLogForTest(log.LvlCrit, log.LvlWarn)

encoded, err := EncodeAuctionCallData(testBid)
assert.Nil(t, err)

assert.Equal(t, common.Hex2Bytes("ca1575540000000000000000000000000000000000000000000000000000000000000020cf5879724726228474db71caf83955a61341f2e5bacaa2b9d37f6e5f48241cbc000000000000000000000000000000000000000000000000000000000000000b00000000000000000000000070997970c51812dc3a010c7d01b50e0d17dc79c80000000000000000000000005fc8d32690cc91d4c39d9d3abcbd16989f87570700000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008ac7230489e8000000000000000000000000000000000000000000000000000000000000009896800000000000000000000000000000000000000000000000000000000000000140000000000000000000000000000000000000000000000000000000000000018000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000004d09de08a00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004145eb1182b8ccffc953ebbb192dd5376cf3a9f53c060a3de6e42526ebe0cb91682e571067ac5aa38d5742f6534d0adc95793443056bd2041d551c7f3aff29967e1c00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004127bba77731bbba25dcde852c54d05eb8d095b2d71b05ff9611ef5a123d1b2af01a7b42d0c8f8ab5ba07456932f4b1d29ff5f6e7b7fb8f59d41d22c4294d23bd91c00000000000000000000000000000000000000000000000000000000000000"), encoded)
}
1 change: 1 addition & 0 deletions kaiax/auction/impl/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ func prep() *AuctionModule {
Chain: backend.BlockChain(),
Backend: apiBackend,
Downloader: fakeDownloader,
NodeKey: testNodeKey,
})
mAuction.bidPool.running = 1
mAuction.bidPool.auctioneer = common.HexToAddress("0x96Bd8E216c0D894C0486341288Bf486d5686C5b6")
Expand Down
23 changes: 15 additions & 8 deletions kaiax/auction/impl/bid_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func (bp *BidPool) SubscribeNewBid(sink chan<- *auction.Bid) event.Subscription

func (bp *BidPool) start() {
// Start the bid pool.
atomic.CompareAndSwapUint32(&bp.running, 0, 1)
// running will be set 1 once it's ready.

bp.wg.Add(2)
go bp.handleBidMsg()
Expand Down Expand Up @@ -185,6 +185,20 @@ func (bp *BidPool) getTargetTxHashMap(num uint64) map[common.Hash]struct{} {
return txHashMap
}

func (bp *BidPool) GetAuctionEntryPoint() common.Address {
bp.auctionInfoMu.RLock()
defer bp.auctionInfoMu.RUnlock()

return bp.auctionEntryPoint
}

func (bp *BidPool) GetTargetTxMap(num uint64) map[common.Hash]*auction.Bid {
bp.bidMu.RLock()
defer bp.bidMu.RUnlock()

return bp.bidTargetMap[num]
}

// AddBid adds a bid to the bid pool.
func (bp *BidPool) AddBid(bid *auction.Bid) (common.Hash, error) {
if atomic.LoadUint32(&bp.running) == 0 {
Expand Down Expand Up @@ -290,13 +304,6 @@ func (bp *BidPool) validateBid(bid *auction.Bid) error {
return nil
}

func (bp *BidPool) stats() int {
bp.bidMu.RLock()
defer bp.bidMu.RUnlock()

return len(bp.bidMap)
}

func (bp *BidPool) validateBidSigs(bid *auction.Bid) error {
bp.auctionInfoMu.RLock()
defer bp.auctionInfoMu.RUnlock()
Expand Down
8 changes: 8 additions & 0 deletions kaiax/auction/impl/bid_pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"crypto/ecdsa"
"fmt"
"math/big"
"sync/atomic"
"testing"

"github.com/golang/mock/gomock"
Expand All @@ -39,12 +40,14 @@ var (
}

// Test private keys
testNodeKey, _ = crypto.HexToECDSA("8b281f165ba7e5cc30bb791f01e1acb62753c1019ddcfc4da2072dc39a052eb8")
testAuctioneerKey, _ = crypto.HexToECDSA("ac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80")
testSearcher1Key, _ = crypto.HexToECDSA("59c6995e998f97a5a0044966f0945389dc9e86dae88c7a8412f4603b6b78690d")
testSearcher2Key, _ = crypto.HexToECDSA("5de4111afa1a4b94908f83103eb1f1706367c2e68ca870fc3fb9a804cdab365a")
testSearcher3Key, _ = crypto.HexToECDSA("7c852118294e51e653712a81e05800f419141751be58f605c371e15141b007a6")

// Addresses derived from private keys
testNode = crypto.PubkeyToAddress(testNodeKey.PublicKey)
testAuctioneer = crypto.PubkeyToAddress(testAuctioneerKey.PublicKey)
testAuctionEntryPoint = common.HexToAddress("0xDc64a140Aa3E981100a9becA4E685f962f0cF6C9")
testSearcher1 = crypto.PubkeyToAddress(testSearcher1Key.PublicKey)
Expand Down Expand Up @@ -164,6 +167,7 @@ func TestBidPool_AddBid(t *testing.T) {

// Start the auction
pool.start()
atomic.StoreUint32(&pool.running, 1)
defer pool.stop()
pool.auctioneer = testAuctioneer
pool.auctionEntryPoint = testAuctionEntryPoint
Expand Down Expand Up @@ -233,6 +237,7 @@ func TestBidPool_RemoveOldBidsByNumber(t *testing.T) {

// Start the auction
pool.start()
atomic.StoreUint32(&pool.running, 1)
defer pool.stop()
pool.auctioneer = testAuctioneer
pool.auctionEntryPoint = testAuctionEntryPoint
Expand Down Expand Up @@ -271,6 +276,7 @@ func TestBidPool_RemoveOldBidsByTxHash(t *testing.T) {

// Start the auction
pool.start()
atomic.StoreUint32(&pool.running, 1)
defer pool.stop()
pool.auctioneer = testAuctioneer
pool.auctionEntryPoint = testAuctionEntryPoint
Expand Down Expand Up @@ -313,6 +319,7 @@ func TestBidPool_ClearBidPool(t *testing.T) {

// Start the auction
pool.start()
atomic.StoreUint32(&pool.running, 1)
defer pool.stop()
pool.auctioneer = testAuctioneer
pool.auctionEntryPoint = testAuctionEntryPoint
Expand Down Expand Up @@ -353,6 +360,7 @@ func TestBidPool_UpdateAuctionInfo(t *testing.T) {

// Start the auction
pool.start()
atomic.StoreUint32(&pool.running, 1)
defer pool.stop()
pool.auctioneer = testAuctioneer
pool.auctionEntryPoint = testAuctionEntryPoint
Expand Down
40 changes: 38 additions & 2 deletions kaiax/auction/impl/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,44 @@ import (
var _ builder.TxBundlingModule = (*AuctionModule)(nil)

func (a *AuctionModule) ExtractTxBundles(txs []*types.Transaction, prevBundles []*builder.Bundle) []*builder.Bundle {
// TODO: implement me
return nil
bundles := []*builder.Bundle{}
curBlock := a.Chain.CurrentBlock()
if curBlock == nil || atomic.LoadUint32(&a.bidPool.running) == 0 {
return bundles
}

miningBlock := curBlock.NumberU64() + 1
bidTargetMap := a.bidPool.GetTargetTxMap(miningBlock)
if bidTargetMap == nil {
return bundles
}

for _, tx := range txs {
txHash := tx.Hash()
bid, ok := bidTargetMap[txHash]
if !ok {
continue
}
b := builder.NewBundle(
builder.NewTxOrGenList(a.GetBidTxGenerator(tx, bid)),
txHash,
true,
)

isConflict := false
for _, prev := range append(prevBundles, bundles...) {
if prev.IsConflict(b) {
isConflict = true
break
}
}
if isConflict {
continue
}
bundles = append(bundles, b)
}

return bundles
}

func (a *AuctionModule) FilterTxs(txs map[common.Address]types.Transactions) {
Expand Down
2 changes: 2 additions & 0 deletions kaiax/auction/impl/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ func TestFilterTxs(t *testing.T) {
Chain: backend.BlockChain(),
Backend: apiBackend,
Downloader: fakeDownloader,
NodeKey: testNodeKey,
})
txs := make(map[common.Address]types.Transactions)

Expand Down Expand Up @@ -104,6 +105,7 @@ func TestFilterTxs_TargetTx(t *testing.T) {
Chain: backend.BlockChain(),
Backend: apiBackend,
Downloader: fakeDownloader,
NodeKey: testNodeKey,
})
txs := make(map[common.Address]types.Transactions)

Expand Down
1 change: 1 addition & 0 deletions kaiax/auction/impl/execution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ func TestUpdateAuctionInfo(t *testing.T) {
Chain: backend.BlockChain(),
Backend: apiBackend,
Downloader: fakeDownloader,
NodeKey: testNodeKey,
})

// Not updated yet
Expand Down
49 changes: 47 additions & 2 deletions kaiax/auction/impl/getter.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,51 @@

package impl

func (a *AuctionModule) Stats() int {
return a.bidPool.stats()
import (
"github.com/kaiachain/kaia/blockchain/system"
"github.com/kaiachain/kaia/blockchain/types"
"github.com/kaiachain/kaia/common"
"github.com/kaiachain/kaia/kaiax/auction"
"github.com/kaiachain/kaia/kaiax/builder"
)

const (
AuctionTxMaxGasLimit = uint64(12_000_000)
)

func (a *AuctionModule) GetBidTxGenerator(tx *types.Transaction, bid *auction.Bid) *builder.TxOrGen {
gen := func(nonce uint64) (*types.Transaction, error) {
var (
chainId = a.InitOpts.ChainConfig.ChainID
signer = types.LatestSignerForChainID(chainId)
auctionEntryPoint = a.bidPool.GetAuctionEntryPoint()
key = a.InitOpts.NodeKey
)

data, err := system.EncodeAuctionCallData(bid)
if err != nil {
return nil, err
}

tx, err := types.NewTransactionWithMap(types.TxTypeEthereumDynamicFee, map[types.TxValueKeyType]interface{}{
types.TxValueKeyNonce: nonce,
types.TxValueKeyTo: &auctionEntryPoint,
types.TxValueKeyAmount: common.Big0,
types.TxValueKeyData: data,
types.TxValueKeyGasLimit: AuctionTxMaxGasLimit,
types.TxValueKeyGasFeeCap: tx.GasFeeCap(),
types.TxValueKeyGasTipCap: tx.GasTipCap(),
types.TxValueKeyAccessList: types.AccessList{},
types.TxValueKeyChainID: chainId,
})
if err != nil {
return nil, err
}

err = tx.Sign(signer, key)

return tx, err
}

return builder.NewTxOrGenFromGen(gen, bid.Hash())
}
Loading