Skip to content

Commit

Permalink
e2e works
Browse files Browse the repository at this point in the history
  • Loading branch information
igorcrevar committed Feb 15, 2024
1 parent 7486023 commit 1614ad6
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 41 deletions.
2 changes: 1 addition & 1 deletion e2e-polybft/e2e/consensus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -872,7 +872,7 @@ func TestE2E_TestValidatorSetPrecompile(t *testing.T) {
response, err := txRelayer.Call(ethgo.ZeroAddress, validatorSetPrecompileTestAddr, hasQuorumFnBytes)
require.NoError(t, err)

return response == "true"
return response == "0x0000000000000000000000000000000000000000000000000000000000000001"
}

sendIncTx := func(validatorID int) {
Expand Down
27 changes: 10 additions & 17 deletions state/runtime/precompiled/validator_set_precompile.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"bytes"
"encoding/binary"
"errors"
"fmt"

"github.com/0xPolygon/polygon-edge/chain"
"github.com/0xPolygon/polygon-edge/consensus/polybft/validator"
Expand Down Expand Up @@ -37,9 +36,15 @@ func (c *validatorSetPrecompile) gas(input []byte, _ *chain.ForksInTime) uint64
// Input must be ABI encoded: address or (address[])
// Output could be an error or ABI encoded "bool" value
func (c *validatorSetPrecompile) run(input []byte, caller types.Address, host runtime.Host) ([]byte, error) {
// if its payable tx we need to look for validator in previous block
blockNumber := uint64(host.GetTxContext().Number)
if !host.GetTxContext().NonPayable {
blockNumber--
}

// isValidator case
if len(input) == 32 {
validatorSet, err := createValidatorSet(host, c.backend) // we are calling validators for previous block
validatorSet, err := createValidatorSet(blockNumber, c.backend) // we are calling validators for previous block
if err != nil {
return nil, err
}
Expand All @@ -58,40 +63,28 @@ func (c *validatorSetPrecompile) run(input []byte, caller types.Address, host ru
return nil, err
}

validatorSet, err := createValidatorSet(host, c.backend)
validatorSet, err := createValidatorSet(blockNumber, c.backend)
if err != nil {
return nil, err
}

fmt.Println("QUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUU")
signers := make(map[types.Address]struct{}, len(addresses))
for _, x := range addresses {
signers[x] = struct{}{}
fmt.Println("HAS QUOORUM", x)
}

for _, x := range validatorSet.Accounts() {
fmt.Println(x.Address.String(), x.VotingPower)
}

if validatorSet.HasQuorum(uint64(host.GetTxContext().Number), signers) {
if validatorSet.HasQuorum(blockNumber, signers) {
return abiBoolTrue, nil
}

return abiBoolFalse, nil
}

func createValidatorSet(host runtime.Host, backend ValidatoSetPrecompileBackend) (validator.ValidatorSet, error) {
func createValidatorSet(blockNumber uint64, backend ValidatoSetPrecompileBackend) (validator.ValidatorSet, error) {
if backend == nil {
return nil, errValidatorSetPrecompileNotEnabled
}

// if its payable tx we need to look for validator in previous block
blockNumber := uint64(host.GetTxContext().Number)
if !host.GetTxContext().NonPayable {
blockNumber--
}

accounts, err := backend.GetValidatorsForBlock(blockNumber)
if err != nil {
return nil, err
Expand Down
49 changes: 26 additions & 23 deletions state/runtime/precompiled/validator_set_precompile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func Test_ValidatorSetPrecompile_run_GetValidatorsForBlockError(t *testing.T) {
}

func Test_ValidatorSetPrecompile_run_IsValidator(t *testing.T) {
addrGood := types.StringToAddress("a")
accounts := getDummyAccountSet()
addrBad := types.StringToAddress("1")
host := newDummyHost(t)
host.context = &runtime.TxContext{
Expand All @@ -61,36 +61,37 @@ func Test_ValidatorSetPrecompile_run_IsValidator(t *testing.T) {
}
backendMock := &validatorSetBackendMock{}

backendMock.On("GetValidatorsForBlock", uint64(host.context.Number-1)).Return(getDummyAccountSet(), error(nil))
backendMock.On("GetValidatorsForBlock", uint64(host.context.Number-1)).Return(accounts, error(nil))

p := &validatorSetPrecompile{
backend: backendMock,
}

v, err := p.run(common.PadLeftOrTrim(addrGood.Bytes(), 32), types.Address{}, host)
require.NoError(t, err)
assert.Equal(t, abiBoolTrue, v)
for _, x := range accounts {
v, err := p.run(common.PadLeftOrTrim(x.Address[:], 32), types.Address{}, host)
require.NoError(t, err)
assert.Equal(t, abiBoolTrue, v)
}

v, err = p.run(common.PadLeftOrTrim(addrBad.Bytes(), 32), types.Address{}, host)
v, err := p.run(common.PadLeftOrTrim(addrBad.Bytes(), 32), types.Address{}, host)
require.NoError(t, err)
assert.Equal(t, abiBoolFalse, v)
}

func Test_ValidatorSetPrecompile_run_HasQuorum(t *testing.T) {
dummy := [32]byte{}
dummy[31] = 32
accounts := getDummyAccountSet()
addrGood := []types.Address{
types.StringToAddress("a"),
types.StringToAddress("b"),
types.StringToAddress("d"),
accounts[0].Address,
accounts[1].Address,
accounts[3].Address,
}
addrBad1 := []types.Address{
types.StringToAddress("a"),
accounts[0].Address,
}
addrBad2 := []types.Address{
types.StringToAddress("a"),
accounts[0].Address,
types.StringToAddress("0"),
types.StringToAddress("d"),
accounts[3].Address,
}
host := newDummyHost(t)
host.context = &runtime.TxContext{
Expand All @@ -99,7 +100,7 @@ func Test_ValidatorSetPrecompile_run_HasQuorum(t *testing.T) {
}
backendMock := &validatorSetBackendMock{}

backendMock.On("GetValidatorsForBlock", uint64(host.context.Number)).Return(getDummyAccountSet(), error(nil))
backendMock.On("GetValidatorsForBlock", uint64(host.context.Number)).Return(accounts, error(nil))

p := &validatorSetPrecompile{
backend: backendMock,
Expand Down Expand Up @@ -129,25 +130,27 @@ func (m *validatorSetBackendMock) GetValidatorsForBlock(blockNumber uint64) (val
}

func getDummyAccountSet() validator.AccountSet {
v, _ := new(big.Int).SetString("1000000000000000000000", 10)

return validator.AccountSet{
&validator.ValidatorMetadata{
Address: types.StringToAddress("a"),
VotingPower: new(big.Int).SetUint64(1),
Address: types.StringToAddress("0xd29f66FEd147B26925DE44Ba468670e921012B6f"),
VotingPower: new(big.Int).Set(v),
IsActive: true,
},
&validator.ValidatorMetadata{
Address: types.StringToAddress("b"),
VotingPower: new(big.Int).SetUint64(1),
Address: types.StringToAddress("0x72aB93bbbc38E90d962dcbf41d973f8C434978e1"),
VotingPower: new(big.Int).Set(v),
IsActive: true,
},
&validator.ValidatorMetadata{
Address: types.StringToAddress("c"),
VotingPower: new(big.Int).SetUint64(1),
Address: types.StringToAddress("0x3b5c82720835d3BAA42eB34E3e4acEE6042A830D"),
VotingPower: new(big.Int).Set(v),
IsActive: true,
},
&validator.ValidatorMetadata{
Address: types.StringToAddress("d"),
VotingPower: new(big.Int).SetUint64(1),
Address: types.StringToAddress("0x87558A1abE10E41a328086474c93449e8A1F8f4e"),
VotingPower: new(big.Int).Set(v),
IsActive: true,
},
}
Expand Down

0 comments on commit 1614ad6

Please sign in to comment.