mirror of
https://gitlab.com/pulsechaincom/erigon-pulse.git
synced 2025-01-10 13:01:21 +00:00
107 lines
12 KiB
Go
107 lines
12 KiB
Go
|
package contract
|
||
|
|
||
|
import (
|
||
|
"math/big"
|
||
|
"strings"
|
||
|
|
||
|
"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/consensus/bor/clerk"
|
||
|
"github.com/ledgerwatch/erigon/rlp"
|
||
|
"github.com/ledgerwatch/log/v3"
|
||
|
)
|
||
|
|
||
|
var (
|
||
|
vABI, _ = abi.JSON(strings.NewReader(ValidatorsetABI))
|
||
|
sABI, _ = abi.JSON(strings.NewReader(StateReceiverABI))
|
||
|
)
|
||
|
|
||
|
func ValidatorSet() abi.ABI {
|
||
|
return vABI
|
||
|
}
|
||
|
|
||
|
func StateReceiver() abi.ABI {
|
||
|
return sABI
|
||
|
}
|
||
|
|
||
|
type GenesisContractsClient struct {
|
||
|
validatorSetABI abi.ABI
|
||
|
stateReceiverABI abi.ABI
|
||
|
ValidatorContract string
|
||
|
StateReceiverContract string
|
||
|
chainConfig *chain.Config
|
||
|
}
|
||
|
|
||
|
const (
|
||
|
ValidatorsetABI = `[{"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":"uint256"}
|
||
|
StateReceiverABI = `[{"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"}]`
|
||
|
)
|
||
|
|
||
|
func NewGenesisContractsClient(
|
||
|
chainConfig *chain.Config,
|
||
|
validatorContract,
|
||
|
stateReceiverContract string,
|
||
|
) *GenesisContractsClient {
|
||
|
return &GenesisContractsClient{
|
||
|
validatorSetABI: ValidatorSet(),
|
||
|
stateReceiverABI: StateReceiver(),
|
||
|
ValidatorContract: validatorContract,
|
||
|
StateReceiverContract: stateReceiverContract,
|
||
|
chainConfig: chainConfig,
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func (gc *GenesisContractsClient) CommitState(
|
||
|
event *clerk.EventRecordWithTime,
|
||
|
syscall consensus.SystemCall,
|
||
|
) error {
|
||
|
eventRecord := event.BuildEventRecord()
|
||
|
|
||
|
recordBytes, err := rlp.EncodeToBytes(eventRecord)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
const method = "commitState"
|
||
|
|
||
|
t := event.Time.Unix()
|
||
|
|
||
|
data, err := gc.stateReceiverABI.Pack(method, big.NewInt(0).SetInt64(t), recordBytes)
|
||
|
if err != nil {
|
||
|
log.Error("Unable to pack tx for commitState", "err", err)
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
log.Trace("→ committing new state", "eventRecord", event.String())
|
||
|
_, err = syscall(libcommon.HexToAddress(gc.StateReceiverContract), data)
|
||
|
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
func (gc *GenesisContractsClient) LastStateId(syscall consensus.SystemCall,
|
||
|
) (*big.Int, error) {
|
||
|
const method = "lastStateId"
|
||
|
|
||
|
data, err := gc.stateReceiverABI.Pack(method)
|
||
|
if err != nil {
|
||
|
log.Error("Unable to pack tx for LastStateId", "err", err)
|
||
|
return nil, err
|
||
|
}
|
||
|
|
||
|
result, err := syscall(libcommon.HexToAddress(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
|
||
|
}
|