Skip to content

Commit

Permalink
Use Edge embedded smart contract definitions in genesis generation (#…
Browse files Browse the repository at this point in the history
…1247)

* Generate artifact for Merkle smart contract

* Rely on embedded smart contracts when generating genesis

* Remove unused function
  • Loading branch information
Stefan-Ethernal authored Feb 23, 2023
1 parent d107eda commit 2d04506
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 37 deletions.
45 changes: 17 additions & 28 deletions command/genesis/polybft_params.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"sort"
"time"

"github.com/0xPolygon/polygon-edge/consensus/polybft/contractsapi"
"github.com/0xPolygon/polygon-edge/consensus/polybft/contractsapi/artifact"

"github.com/0xPolygon/polygon-edge/chain"
Expand Down Expand Up @@ -177,59 +178,47 @@ func (p *genesisParams) generatePolyBftChainConfig() error {

func (p *genesisParams) deployContracts(totalStake *big.Int) (map[types.Address]*chain.GenesisAccount, error) {
genesisContracts := []struct {
name string
relativePath string
address types.Address
artifact *artifact.Artifact
address types.Address
}{
{
// Validator contract
name: "ChildValidatorSet",
relativePath: "child/ChildValidatorSet.sol",
address: contracts.ValidatorSetContract,
// ChildValidatorSet contract
artifact: contractsapi.ChildValidatorSet,
address: contracts.ValidatorSetContract,
},
{
// State receiver contract
name: "StateReceiver",
relativePath: "child/StateReceiver.sol",
address: contracts.StateReceiverContract,
artifact: contractsapi.StateReceiver,
address: contracts.StateReceiverContract,
},
{
// Native Token contract (Matic ERC-20)
name: "MRC20",
relativePath: "child/MRC20.sol",
address: contracts.NativeTokenContract,
artifact: contractsapi.MRC20,
address: contracts.NativeTokenContract,
},
{
// BLS contract
name: "BLS",
relativePath: "common/BLS.sol",
address: contracts.BLSContract,
artifact: contractsapi.BLS,
address: contracts.BLSContract,
},
{
// Merkle contract
name: "Merkle",
relativePath: "common/Merkle.sol",
address: contracts.MerkleContract,
artifact: contractsapi.Merkle,
address: contracts.MerkleContract,
},
{
// L2StateSender contract
name: "L2StateSender",
relativePath: "child/L2StateSender.sol",
address: contracts.L2StateSenderContract,
artifact: contractsapi.L2StateSender,
address: contracts.L2StateSenderContract,
},
}

allocations := make(map[types.Address]*chain.GenesisAccount, len(genesisContracts))

for _, contract := range genesisContracts {
artifact, err := artifact.ReadArtifact(p.smartContractsRootPath, contract.relativePath, contract.name)
if err != nil {
return nil, err
}

allocations[contract.address] = &chain.GenesisAccount{
Balance: big.NewInt(0),
Code: artifact.DeployedBytecode,
Code: contract.artifact.DeployedBytecode,
}
}

Expand Down
9 changes: 0 additions & 9 deletions consensus/polybft/contractsapi/artifact/artifacts.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,6 @@ import (
"github.com/umbracle/ethgo/abi"
)

func ReadArtifact(rootFolder, contractPath, contractName string) (*Artifact, error) {
data, err := ReadArtifactData(rootFolder, contractPath, contractName)
if err != nil {
return nil, err
}

return DecodeArtifact(data)
}

func ReadArtifactData(rootFolder, contractPath, contractName string) ([]byte, error) {
fileName := filepath.Join(rootFolder, contractPath, fmt.Sprintf("%s.json", contractName))

Expand Down
4 changes: 4 additions & 0 deletions consensus/polybft/contractsapi/artifacts-gen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ func main() {
"common/BN256G2.sol",
"BN256G2",
},
{
"common/Merkle.sol",
"Merkle",
},
{
"child/StateReceiver.sol",
"StateReceiver",
Expand Down
1 change: 1 addition & 0 deletions consensus/polybft/contractsapi/gen_sc_data.go

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions consensus/polybft/contractsapi/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ var (
BLS *artifact.Artifact
BLS256 *artifact.Artifact
System *artifact.Artifact
Merkle *artifact.Artifact
ChildValidatorSet *artifact.Artifact
MRC20 *artifact.Artifact

Expand Down Expand Up @@ -59,6 +60,11 @@ func init() {
panic(err)
}

Merkle, err = artifact.DecodeArtifact([]byte(MerkleArtifact))
if err != nil {
panic(err)
}

StateSender, err = artifact.DecodeArtifact([]byte(StateSenderArtifact))
if err != nil {
panic(err)
Expand Down

0 comments on commit 2d04506

Please sign in to comment.