refactor: DA

This commit is contained in:
MiniFrenchBread 2025-04-16 08:33:23 +08:00
parent f77f62ec5f
commit 7e03062283
7 changed files with 105 additions and 56 deletions

File diff suppressed because one or more lines are too long

View file

@ -69,7 +69,7 @@ func (suite *DASignersTestSuite) registerSigner(testSigner common.Address, sk *b
suite.Assert().NoError(err)
oldLogs := suite.statedb.Logs()
_, err = suite.runTx(input, testSigner, 10000000, uint256.NewInt(0), false)
_, err = suite.runTx(input, suite.dasigners.getRegistry(), 10000000, uint256.NewInt(0), false)
suite.Assert().NoError(err)
logs := suite.statedb.Logs()
suite.Assert().EqualValues(len(logs), len(oldLogs)+2)
@ -110,18 +110,20 @@ func (suite *DASignersTestSuite) updateSocket(testSigner common.Address, signer
signer.Socket = "0.0.0.0:2345"
}
func (suite *DASignersTestSuite) registerEpoch(testSigner common.Address, sk *big.Int) {
func (suite *DASignersTestSuite) registerEpoch(testSigner common.Address, sk *big.Int, votes *big.Int) {
epoch := suite.dasigners.epochNumber(suite.evm) + 1
hash := dasigners.EpochRegistrationHash(testSigner, epoch, suite.evm.chainConfig.ChainID)
signature := new(bn254.G1Affine).ScalarMultiplication(hash, sk)
input, err := suite.abi.Pack(
"registerNextEpoch",
testSigner,
dasigners.NewBN254G1Point(bn254util.SerializeG1(signature)),
votes,
)
suite.Assert().NoError(err)
_, err = suite.runTx(input, testSigner, 10000000, uint256.NewInt(0), false)
_, err = suite.runTx(input, suite.dasigners.getRegistry(), 10000000, uint256.NewInt(0), false)
suite.Assert().NoError(err)
}
@ -264,8 +266,8 @@ func (suite *DASignersTestSuite) Test_DASigners() {
signer2 := suite.registerSigner(suite.signerTwo, big.NewInt(11))
suite.updateSocket(suite.signerOne, signer1)
suite.updateSocket(suite.signerTwo, signer2)
suite.registerEpoch(suite.signerOne, big.NewInt(1))
suite.registerEpoch(suite.signerTwo, big.NewInt(11))
suite.registerEpoch(suite.signerOne, big.NewInt(1), big.NewInt(1))
suite.registerEpoch(suite.signerTwo, big.NewInt(11), big.NewInt(2))
// move to next epochs
daparams := suite.dasigners.params()
suite.queryEpochNumber(suite.signerOne, big.NewInt(0))
@ -274,8 +276,8 @@ func (suite *DASignersTestSuite) Test_DASigners() {
suite.queryEpochNumber(suite.signerOne, big.NewInt(1))
suite.makeEpoch(suite.signerOne)
suite.queryEpochNumber(suite.signerOne, big.NewInt(1))
suite.registerEpoch(suite.signerOne, big.NewInt(1))
suite.registerEpoch(suite.signerTwo, big.NewInt(11))
suite.registerEpoch(suite.signerOne, big.NewInt(1), big.NewInt(1))
suite.registerEpoch(suite.signerTwo, big.NewInt(11), big.NewInt(2))
// move to epoch 2
suite.evm.Context.BlockNumber = suite.evm.Context.BlockNumber.Add(suite.evm.Context.BlockNumber, daparams.EpochBlocks)
suite.makeEpoch(suite.signerOne)
@ -307,10 +309,8 @@ func (suite *DASignersTestSuite) Test_DASigners() {
twoPos = min(twoPos, i)
}
}
suite.Assert().EqualValues(cnt[suite.signerOne], len(quorum)/2)
suite.Assert().EqualValues(cnt[suite.signerTwo], len(quorum)/2)
// suite.Assert().EqualValues(cnt[suite.signerOne], len(quorum)/3)
// suite.Assert().EqualValues(cnt[suite.signerTwo], len(quorum)*2/3)
suite.Assert().EqualValues(cnt[suite.signerOne], len(quorum)/3)
suite.Assert().EqualValues(cnt[suite.signerTwo], len(quorum)*2/3)
bitMap := make([]byte, len(quorum)/8)
bitMap[onePos/8] |= 1 << (onePos % 8)
@ -321,8 +321,7 @@ func (suite *DASignersTestSuite) Test_DASigners() {
}{
AggPkG1: dasigners.NewBN254G1Point(bn254util.SerializeG1(new(bn254.G1Affine).ScalarMultiplication(bn254util.GetG1Generator(), big.NewInt(1)))),
Total: big.NewInt(int64(len(quorum))),
Hit: big.NewInt(int64(len(quorum) / 2)),
// Hit: big.NewInt(int64(len(quorum) / 3)),
Hit: big.NewInt(int64(len(quorum) / 3)),
})
bitMap[twoPos/8] |= 1 << (twoPos % 8)

File diff suppressed because one or more lines are too long

View file

@ -71,12 +71,13 @@ func SerializeG2(p BN254G2Point) []byte {
var (
signerKey = []byte{0x00} // signer => registered signer info
quorumKey = []byte{0x01} // epoch => quorumId => quorum
registrationKey = []byte{0x02} // signer => signature hash
quorumCountKey = []byte{0x03} // epoch => quorum count
epochNumberKey = []byte{0x04} // epoch number
epochBlockKey = []byte{0x05} // epoch number => block number
epochRegistrationKey = []byte{0x06} // epoch number => registration count
epochRegisteredSignerKey = []byte{0x07} // epoch number => index => signer
registrationKey = []byte{0x02} // epoch => signer => signature(vrf)
votesKey = []byte{0x03} // epoch => signer => votes
quorumCountKey = []byte{0x04} // epoch => quorum count
epochNumberKey = []byte{0x05} // epoch number
epochBlockKey = []byte{0x06} // epoch number => block number
epochRegistrationKey = []byte{0x07} // epoch number => registration count
epochRegisteredSignerKey = []byte{0x08} // epoch number => index => signer
)
func SignerKey(account common.Address) common.Hash {
@ -91,6 +92,10 @@ func RegistrationKey(epochNumber uint64, account common.Address) common.Hash {
return crypto.Keccak256Hash(append(append(registrationKey, common.Uint64ToBytes(epochNumber)...), account.Bytes()...))
}
func VotesKey(epochNumber uint64, account common.Address) common.Hash {
return crypto.Keccak256Hash(append(append(votesKey, common.Uint64ToBytes(epochNumber)...), account.Bytes()...))
}
func QuorumCountKey(epochNumber uint64) common.Hash {
return crypto.Keccak256Hash(append(quorumCountKey, common.Uint64ToBytes(epochNumber)...))
}

View file

@ -4,4 +4,5 @@ import "errors"
var (
ErrSenderNotOrigin = errors.New("sender not origin")
ErrSenderNotRegistry = errors.New("sender not registry")
)

View file

@ -336,6 +336,11 @@
},
{
"inputs": [
{
"internalType": "address",
"name": "signer",
"type": "address"
},
{
"components": [
{
@ -352,6 +357,11 @@
"internalType": "struct BN254.G1Point",
"name": "_signature",
"type": "tuple"
},
{
"internalType": "uint256",
"name": "votes",
"type": "uint256"
}
],
"name": "registerNextEpoch",

View file

@ -24,7 +24,7 @@ interface IDASigners {
}
struct Params {
uint tokensPerVote;
uint tokensPerVote; // deprecated
uint maxVotesPerSigner;
uint maxQuorums;
uint epochBlocks;
@ -75,7 +75,11 @@ interface IDASigners {
uint _epoch
) external view returns (bool);
function registerNextEpoch(BN254.G1Point memory _signature) external;
function registerNextEpoch(
address signer,
BN254.G1Point memory _signature,
uint votes
) external;
function makeEpoch() external;