mirror of
https://gitlab.com/pulsechaincom/erigon-pulse.git
synced 2025-01-05 10:32:19 +00:00
88 lines
12 KiB
Go
88 lines
12 KiB
Go
|
package bor
|
||
|
|
||
|
import (
|
||
|
"math/big"
|
||
|
"strings"
|
||
|
|
||
|
"github.com/ledgerwatch/log/v3"
|
||
|
|
||
|
"github.com/ledgerwatch/erigon-lib/chain"
|
||
|
libcommon "github.com/ledgerwatch/erigon-lib/common"
|
||
|
"github.com/ledgerwatch/erigon/accounts/abi"
|
||
|
"github.com/ledgerwatch/erigon/consensus"
|
||
|
"github.com/ledgerwatch/erigon/rlp"
|
||
|
)
|
||
|
|
||
|
const (
|
||
|
validatorSetABIJSON = `[{"constant":true,"inputs":[],"name":"SPRINT","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"SYSTEM_ADDRESS","outputs":[{"internalType":"address","name":"","type":"address"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"CHAIN","outputs":[{"internalType":"bytes32","name":"","type":"bytes32"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"FIRST_END_BLOCK","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"internalType":"uint256","name":"","type":"uint256"},{"internalType":"uint256","name":"","type":"uint256"}],"name":"producers","outputs":[{"internalType":"uint256","name":"id","type":"uint256"},{"internalType":"uint256","name":"power","type":"uint256"},{"internalType":"address","name":"signer","type":"address"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"ROUND_TYPE","outputs":[{"internalType":"bytes32","name":"","type":"bytes32"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"BOR_ID","outputs":[{"internalType":"bytes32","name":"","type":"bytes32"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"internalType":"uint256","name":"","type":"uint256"}],"name":"spanNumbers","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"VOTE_TYPE","outputs":[{"internalType":"uint8","name":"","type":"uint8"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"internalType":"uint256","name":"","type":"uint256"},{"internalType":"uint256","name":"","type":"uint256"}],"name":"validators","outputs":[{"internalType":"uint256","name":"id","type":"uint256"},{"internalType":"uint256","name":"power","type":"uint256"},{"internalType":"address","name":"signer","type":"address"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"internalType":"uint256","name":"","type":"uint256"}],"name":"spans","outputs":[{"internalType":"uint256","name":"number","type":"uint256"},{"internalType":"uint256","name":"startBlock","type":"uint256"},{"internalType":"uint256","name":"endBlock","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"inputs":[],"payable":false,"stateMutability":"nonpayable","type":"constructor"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"uint256","name":"id","type":"uint256"},{"indexed":true,"internalType":"uint256","name":"startBlock","type":"uint256"},{"indexed":true,"internalType":"uint256","name":"endBlock","type":"uint256"}],"name":"NewSpan","type":"event"},{"constant":true,"inputs":[],"name":"currentSprint","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"internalType":"uint256","name":"span","type":"uint256"}],"name":"getSpan","outputs":[{"internalType":"uint256","name":"number","type":"uint256"},{"internalType":"uint256","name":"startBlock","type":"uint256"},{"internalType":"uint256","name":"endBlock","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"getCurrentSpan","outputs":[{"internalType":"uint256","name":"number","type":"uint256"},{"internalType":"uint256","name":"startBlock","type":"uint256"},{"internalType":"uint256","name":"endBlock","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"getNextSpan","outputs":[{"internalType":"uint256","name":"number","type":"uint256"},{"internalType":"uint256","name":"startBlock","type":"uint256"},{"internalType":"uint256","name":"endBlock","type":"uint2
|
||
|
stateReceiverABIJSON = `[{"constant":true,"inputs":[],"name":"SYSTEM_ADDRESS","outputs":[{"internalType":"address","name":"","type":"address"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"lastStateId","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"internalType":"uint256","name":"syncTime","type":"uint256"},{"internalType":"bytes","name":"recordBytes","type":"bytes"}],"name":"commitState","outputs":[{"internalType":"bool","name":"success","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"}]`
|
||
|
)
|
||
|
|
||
|
var (
|
||
|
validatorSetABI, _ = abi.JSON(strings.NewReader(validatorSetABIJSON))
|
||
|
stateReceiverABI, _ = abi.JSON(strings.NewReader(stateReceiverABIJSON))
|
||
|
)
|
||
|
|
||
|
func GenesisContractValidatorSetABI() abi.ABI {
|
||
|
return validatorSetABI
|
||
|
}
|
||
|
func GenesisContractStateReceiverABI() abi.ABI {
|
||
|
return stateReceiverABI
|
||
|
}
|
||
|
|
||
|
type GenesisContracts interface {
|
||
|
CommitState(event rlp.RawValue, syscall consensus.SystemCall) error
|
||
|
LastStateId(syscall consensus.SystemCall) (*big.Int, error)
|
||
|
}
|
||
|
|
||
|
type GenesisContractsClient struct {
|
||
|
validatorSetABI abi.ABI
|
||
|
stateReceiverABI abi.ABI
|
||
|
ValidatorContract libcommon.Address
|
||
|
StateReceiverContract libcommon.Address
|
||
|
chainConfig *chain.Config
|
||
|
logger log.Logger
|
||
|
}
|
||
|
|
||
|
func NewGenesisContractsClient(
|
||
|
chainConfig *chain.Config,
|
||
|
validatorContract,
|
||
|
stateReceiverContract string,
|
||
|
logger log.Logger,
|
||
|
) *GenesisContractsClient {
|
||
|
return &GenesisContractsClient{
|
||
|
validatorSetABI: GenesisContractValidatorSetABI(),
|
||
|
stateReceiverABI: GenesisContractStateReceiverABI(),
|
||
|
ValidatorContract: libcommon.HexToAddress(validatorContract),
|
||
|
StateReceiverContract: libcommon.HexToAddress(stateReceiverContract),
|
||
|
chainConfig: chainConfig,
|
||
|
logger: logger,
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func (gc *GenesisContractsClient) CommitState(event rlp.RawValue, syscall consensus.SystemCall) error {
|
||
|
_, err := syscall(gc.StateReceiverContract, event)
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
func (gc *GenesisContractsClient) LastStateId(syscall consensus.SystemCall) (*big.Int, error) {
|
||
|
const method = "lastStateId"
|
||
|
|
||
|
data, err := gc.stateReceiverABI.Pack(method)
|
||
|
if err != nil {
|
||
|
gc.logger.Error("[bor] Unable to pack tx for LastStateId", "err", err)
|
||
|
return nil, err
|
||
|
}
|
||
|
|
||
|
result, err := syscall(gc.StateReceiverContract, data)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
|
||
|
var ret = new(*big.Int)
|
||
|
if err := gc.stateReceiverABI.UnpackIntoInterface(ret, method, result); err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
return *ret, nil
|
||
|
}
|