Skip to content

Commit

Permalink
a little bit more of a readability
Browse files Browse the repository at this point in the history
  • Loading branch information
igorcrevar committed Feb 19, 2024
1 parent c2d5578 commit 543b51e
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,64 +3,64 @@
"contractName": "TestValidatorSetPrecompile",
"sourceName": "consensus/polybft/contractsapi/test-contracts/TestValidatorSetContract.sol",
"abi": [
{
"inputs":[
],
"name":"VALIDATOR_SET_PRECOMPILE",
"outputs":[
{
"internalType":"address",
"name":"",
"type":"address"
}
],
"stateMutability":"view",
"type":"function"
{
"inputs":[

],
"name":"VALIDATOR_SET_PRECOMPILE",
"outputs":[
{
"internalType":"address",
"name":"",
"type":"address"
}
],
"stateMutability":"view",
"type":"function"
},
{
"inputs":[
],
"name":"VALIDATOR_SET_PRECOMPILE_GAS",
"outputs":[
{
"internalType":"uint256",
"name":"",
"type":"uint256"
}
],
"stateMutability":"view",
"type":"function"
"inputs":[

],
"name":"VALIDATOR_SET_PRECOMPILE_GAS",
"outputs":[
{
"internalType":"uint256",
"name":"",
"type":"uint256"
}
],
"stateMutability":"view",
"type":"function"
},
{
"inputs":[
],
"name":"hasQuorum",
"outputs":[
{
"internalType":"bool",
"name":"",
"type":"bool"
}
],
"stateMutability":"view",
"type":"function"
"inputs":[

],
"name":"hasQuorum",
"outputs":[
{
"internalType":"bool",
"name":"",
"type":"bool"
}
],
"stateMutability":"view",
"type":"function"
},
{
"inputs":[
],
"name":"inc",
"outputs":[
],
"stateMutability":"nonpayable",
"type":"function"
"inputs":[

],
"name":"inc",
"outputs":[

],
"stateMutability":"nonpayable",
"type":"function"
}
],
"bytecode": "0x608060405234801561000f575f80fd5b506106ea8061001d5f395ff3fe608060405234801561000f575f80fd5b506004361061004a575f3560e01c8063371303c01461004e5780635e4f948814610058578063815b4d1b1461007657806384005d5614610094575b5f80fd5b6100566100b2565b005b6100606102a8565b60405161006d91906103ae565b60405180910390f35b61007e6102ae565b60405161008b91906103e1565b60405180910390f35b61009c610368565b6040516100a99190610412565b60405180910390f35b5f8061204073ffffffffffffffffffffffffffffffffffffffff16620249f0336040516020016100e291906103ae565b6040516020818303038152906040526040516100fe9190610497565b5f604051808303818686fa925050503d805f8114610137576040519150601f19603f3d011682016040523d82523d5f602084013e61013c565b606091505b50915091505f8180602001905181019061015691906104db565b90508280156101625750805b6101a1576040517f08c379a000000000000000000000000000000000000000000000000000000000815260040161019890610560565b60405180910390fd5b5f803373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020015f205f9054906101000a900460ff166102a357600133908060018154018082558091505060019003905f5260205f20015f9091909190916101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff16021790555060015f803373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020015f205f6101000a81548160ff0219169083151502179055505b505050565b61204081565b5f805f61204073ffffffffffffffffffffffffffffffffffffffff16620249f060016040516020016102e09190610694565b6040516020818303038152906040526040516102fc9190610497565b5f604051808303818686fa925050503d805f8114610335576040519150601f19603f3d011682016040523d82523d5f602084013e61033a565b606091505b50915091505f8180602001905181019061035491906104db565b90508280156103605750805b935050505090565b620249f081565b5f73ffffffffffffffffffffffffffffffffffffffff82169050919050565b5f6103988261036f565b9050919050565b6103a88161038e565b82525050565b5f6020820190506103c15f83018461039f565b92915050565b5f8115159050919050565b6103db816103c7565b82525050565b5f6020820190506103f45f8301846103d2565b92915050565b5f819050919050565b61040c816103fa565b82525050565b5f6020820190506104255f830184610403565b92915050565b5f81519050919050565b5f81905092915050565b5f5b8381101561045c578082015181840152602081019050610441565b5f8484015250505050565b5f6104718261042b565b61047b8185610435565b935061048b81856020860161043f565b80840191505092915050565b5f6104a28284610467565b915081905092915050565b5f80fd5b6104ba816103c7565b81146104c4575f80fd5b50565b5f815190506104d5816104b1565b92915050565b5f602082840312156104f0576104ef6104ad565b5b5f6104fd848285016104c7565b91505092915050565b5f82825260208201905092915050565b7f76616c696461746f72206f6e6c790000000000000000000000000000000000005f82015250565b5f61054a600e83610506565b915061055582610516565b602082019050919050565b5f6020820190508181035f8301526105778161053e565b9050919050565b5f81549050919050565b5f82825260208201905092915050565b5f819050815f5260205f209050919050565b6105b38161038e565b82525050565b5f6105c483836105aa565b60208301905092915050565b5f815f1c9050919050565b5f73ffffffffffffffffffffffffffffffffffffffff82169050919050565b5f61060c610607836105d0565b6105db565b9050919050565b5f61061e82546105fa565b9050919050565b5f600182019050919050565b5f61063b8261057e565b6106458185610588565b935061065083610598565b805f5b838110156106875761066482610613565b61066e88826105b9565b975061067983610625565b925050600181019050610653565b5085935050505092915050565b5f6020820190508181035f8301526106ac8184610631565b90509291505056fea2646970667358221220efe83875fb1246787277c88a460f768e19f019c8e2e557e6d05731425a3210f864736f6c63430008180033",
],
"bytecode": "0x608060405234801561000f575f80fd5b506106508061001d5f395ff3fe608060405234801561000f575f80fd5b5060043610610034575f3560e01c8063371303c014610038578063815b4d1b14610042575b5f80fd5b610040610060565b005b61004a610251565b6040516100579190610320565b60405180910390f35b5f8061204073ffffffffffffffffffffffffffffffffffffffff16620249f0336040516020016100909190610378565b6040516020818303038152906040526040516100ac91906103fd565b5f604051808303818686fa925050503d805f81146100e5576040519150601f19603f3d011682016040523d82523d5f602084013e6100ea565b606091505b509150915081801561010c57508080602001905181019061010b9190610441565b5b61014b576040517f08c379a0000000000000000000000000000000000000000000000000000000008152600401610142906104c6565b60405180910390fd5b5f803373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020015f205f9054906101000a900460ff1661024d57600133908060018154018082558091505060019003905f5260205f20015f9091909190916101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff16021790555060015f803373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020015f205f6101000a81548160ff0219169083151502179055505b5050565b5f805f61204073ffffffffffffffffffffffffffffffffffffffff16620249f0600160405160200161028391906105fa565b60405160208183030381529060405260405161029f91906103fd565b5f604051808303818686fa925050503d805f81146102d8576040519150601f19603f3d011682016040523d82523d5f602084013e6102dd565b606091505b50915091508180156102ff5750808060200190518101906102fe9190610441565b5b9250505090565b5f8115159050919050565b61031a81610306565b82525050565b5f6020820190506103335f830184610311565b92915050565b5f73ffffffffffffffffffffffffffffffffffffffff82169050919050565b5f61036282610339565b9050919050565b61037281610358565b82525050565b5f60208201905061038b5f830184610369565b92915050565b5f81519050919050565b5f81905092915050565b5f5b838110156103c25780820151818401526020810190506103a7565b5f8484015250505050565b5f6103d782610391565b6103e1818561039b565b93506103f18185602086016103a5565b80840191505092915050565b5f61040882846103cd565b915081905092915050565b5f80fd5b61042081610306565b811461042a575f80fd5b50565b5f8151905061043b81610417565b92915050565b5f6020828403121561045657610455610413565b5b5f6104638482850161042d565b91505092915050565b5f82825260208201905092915050565b7f76616c696461746f7200000000000000000000000000000000000000000000005f82015250565b5f6104b060098361046c565b91506104bb8261047c565b602082019050919050565b5f6020820190508181035f8301526104dd816104a4565b9050919050565b5f81549050919050565b5f82825260208201905092915050565b5f819050815f5260205f209050919050565b61051981610358565b82525050565b5f61052a8383610510565b60208301905092915050565b5f815f1c9050919050565b5f73ffffffffffffffffffffffffffffffffffffffff82169050919050565b5f61057261056d83610536565b610541565b9050919050565b5f6105848254610560565b9050919050565b5f600182019050919050565b5f6105a1826104e4565b6105ab81856104ee565b93506105b6836104fe565b805f5b838110156105ed576105ca82610579565b6105d4888261051f565b97506105df8361058b565b9250506001810190506105b9565b5085935050505092915050565b5f6020820190508181035f8301526106128184610597565b90509291505056fea26469706673582212207ce9734a4cb70866c45912245079c5f01a9c3f3b2cae0888b8afbbec2c224b6c64736f6c63430008180033",
"linkReferences": {},
"deployedLinkReferences": {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
pragma solidity ^0.8.0;

contract TestValidatorSetPrecompile {
address public constant VALIDATOR_SET_PRECOMPILE = 0x0000000000000000000000000000000000002040;
uint256 public constant VALIDATOR_SET_PRECOMPILE_GAS = 150000;
address constant VALIDATOR_SET_PRECOMPILE = 0x0000000000000000000000000000000000002040;
uint256 constant VALIDATOR_SET_PRECOMPILE_GAS = 150000;

mapping(address => bool) voteMap;
address[] votes;
Expand All @@ -14,8 +14,7 @@ contract TestValidatorSetPrecompile {
(bool callSuccess, bytes memory returnData) = VALIDATOR_SET_PRECOMPILE.staticcall{
gas: VALIDATOR_SET_PRECOMPILE_GAS
}(abi.encode(msg.sender));
bool state = abi.decode(returnData, (bool));
require(callSuccess && state, "validator only");
require(callSuccess && abi.decode(returnData, (bool)), "validator");
_;
}

Expand All @@ -30,7 +29,6 @@ contract TestValidatorSetPrecompile {
(bool callSuccess, bytes memory returnData) = VALIDATOR_SET_PRECOMPILE.staticcall{
gas: VALIDATOR_SET_PRECOMPILE_GAS
}(abi.encode(votes));
bool state = abi.decode(returnData, (bool));
return callSuccess && state;
return callSuccess && abi.decode(returnData, (bool));
}
}
13 changes: 9 additions & 4 deletions state/runtime/precompiled/validator_set_precompile.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import (
"github.com/hashicorp/go-hclog"
)

const addrOffset = 32 - types.AddressLength

var (
errValidatorSetPrecompileNotEnabled = errors.New("validator set precompile is not enabled")
)
Expand Down Expand Up @@ -116,7 +118,7 @@ func abiDecodeAddresses(input []byte) ([]types.Address, error) {

res := make([]types.Address, size)
for i, offset := 0, 32; offset < len(input); i, offset = i+1, offset+32 {
res[i] = types.Address(input[offset+12 : offset+32])
res[i] = types.Address(input[offset+addrOffset : offset+32])
}

return res, nil
Expand All @@ -126,10 +128,13 @@ func abiEncodeAddresses(addrs []types.Address) []byte {
res := make([]byte, len(addrs)*32+64)
res[31] = 32

binary.BigEndian.PutUint32(res[32+28:64], uint32(len(addrs)))
binary.BigEndian.PutUint32(res[60:64], uint32(len(addrs))) // 60 == 32 + 28

offset := 64

for i, a := range addrs {
copy(res[64+i*32+12:64+(i+1)*32], a.Bytes())
for _, addr := range addrs {
copy(res[offset+addrOffset:offset+32], addr.Bytes())
offset += 32
}

return res
Expand Down

0 comments on commit 543b51e

Please sign in to comment.