Skip to content

Commit

Permalink
save ABI information about "related" contracts
Browse files Browse the repository at this point in the history
  • Loading branch information
hayeah committed Jan 23, 2018
1 parent b6561c3 commit 62fbaad
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 16 deletions.
67 changes: 57 additions & 10 deletions compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ type rawCompilerOutput struct {
Contracts map[string]contract.RawCompiledContract
}

func (o *rawCompilerOutput) CompiledContracts() map[string]contract.CompiledContract {
contracts := make(map[string]contract.CompiledContract)
func (o *rawCompilerOutput) CompiledContracts() contract.CompiledContracts {
contracts := make(contract.CompiledContracts)

for name, rawContract := range o.Contracts {
if len(rawContract.Bin) == 0 {
Expand All @@ -32,14 +32,15 @@ func (o *rawCompilerOutput) CompiledContracts() map[string]contract.CompiledCont
contractName = parts[1]
}

compiledContract := contract.CompiledContract{
compiledContract := &contract.CompiledContract{
Source: name,
Name: contractName,
Bin: rawContract.Bin,
BinKeccak256: rawContract.BinHash256(),
ABI: rawContract.Metadata.Output.ABI,
}

contracts[contractName] = compiledContract
contracts[name] = compiledContract
}

return contracts
Expand All @@ -64,27 +65,59 @@ type Compiler struct {
Filename string
Opts CompilerOptions
Repo *contract.ContractsRepository

compiledContracts contract.CompiledContracts
}

func (c *Compiler) mainContractSource() string {
mainContractName := basenameNoExt(c.Filename)

return c.Filename + ":" + mainContractName
}

func (c *Compiler) mainContractName() string {
mainContractName := basenameNoExt(c.Filename)
return mainContractName
}

// Compile returns only the contract that has the same name as the source file
func (c *Compiler) Compile() (*contract.CompiledContract, error) {
mainContractName := basenameNoExt(c.Filename)

contracts, err := c.CompileAll()
contracts, err := c.getCompiledContracts()
if err != nil {
return nil, err
}

contract, ok := contracts[mainContractName]
contractSrc := c.mainContractSource()
contract, ok := contracts[contractSrc]
if !ok {
return nil, errors.Errorf("cannot find contract: %s", mainContractName)
return nil, errors.Errorf("cannot find contract: %s", c.mainContractName())
}

return contract, nil
}

func (c *Compiler) RelatedContracts() (contract.CompiledContracts, error) {
contracts, err := c.getCompiledContracts()
if err != nil {
return nil, err
}

return &contract, nil
contractSrc := c.mainContractSource()
relatedContracts := make(contract.CompiledContracts)
for name, contract := range contracts {
if name == contractSrc {
continue
}

relatedContracts[name] = contract
}

return relatedContracts, nil
}

// CompileAll returns all contracts in a source file
func (c *Compiler) CompileAll() (map[string]contract.CompiledContract, error) {
func (c *Compiler) compileAll() (contract.CompiledContracts, error) {
_, err := os.Stat(c.Filename)

if err != nil && os.IsNotExist(err) {
Expand All @@ -99,6 +132,20 @@ func (c *Compiler) CompileAll() (map[string]contract.CompiledContract, error) {
return output.CompiledContracts(), nil
}

func (c *Compiler) getCompiledContracts() (contract.CompiledContracts, error) {
if c.compiledContracts != nil {
return c.compiledContracts, nil
}

contracts, err := c.compileAll()
if err != nil {
return nil, err
}

c.compiledContracts = contracts
return contracts, nil
}

func (c *Compiler) execSolc() (*rawCompilerOutput, error) {
opts := c.Opts

Expand Down
8 changes: 5 additions & 3 deletions contract/compiledContract.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,11 @@ type ABIType struct {
}

type CompiledContract struct {
Name string `json:"name"`
ABI []ABIDefinition `json:"abi"`
Bin Bytes `json:"bin"`
// Where the contract is defined
Source string `json:"source"`
Name string `json:"name"`
ABI []ABIDefinition `json:"abi"`
Bin Bytes `json:"bin"`
// KECAAK256 of bytecode without auxdata
BinKeccak256 Bytes `json:"binhash"`
}
Expand Down
10 changes: 10 additions & 0 deletions contract/contractsRepo.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
"github.com/pkg/errors"
)

type CompiledContracts map[string]*CompiledContract

type DeployedContracts map[string]*DeployedContract

type DeployedContract struct {
Expand All @@ -30,6 +32,9 @@ type ContractsRepository struct {
filepath string
Contracts DeployedContracts `json:"contracts"`
Libraries DeployedContracts `json:"libraries"`

// ABI definitions related to the contracts, but not deployed.
Related CompiledContracts `json:"related"`
}

func OpenContractsRepository(filepath string) (repo *ContractsRepository, err error) {
Expand All @@ -39,6 +44,7 @@ func OpenContractsRepository(filepath string) (repo *ContractsRepository, err er
filepath: filepath,
Contracts: make(DeployedContracts),
Libraries: make(DeployedContracts),
Related: make(CompiledContracts),
}, nil
}

Expand All @@ -65,6 +71,10 @@ func OpenContractsRepository(filepath string) (repo *ContractsRepository, err er
repo.Contracts = make(DeployedContracts)
}

if repo.Related == nil {
repo.Related = make(CompiledContracts)
}

return
}

Expand Down
20 changes: 19 additions & 1 deletion deploy.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package solar

import (
"github.com/qtumproject/solar/contract"
"fmt"
"log"
"strings"

"github.com/qtumproject/solar/contract"

"github.com/pkg/errors"
)

Expand Down Expand Up @@ -80,6 +81,23 @@ func init() {
return
}

// Add related contracts to repo
relatedContracts, err := compiler.RelatedContracts()
if err != nil {
return err
}

if len(relatedContracts) > 0 {
for name, c := range relatedContracts {
repo.Related[name] = c
}

err = repo.Commit()
if err != nil {
return
}
}

newContracts := repo.UnconfirmedContracts()
if *noconfirm == false && len(newContracts) != 0 {
// Force local chain to generate a block immediately.
Expand Down
4 changes: 2 additions & 2 deletions deployer/qtum/qtumDeployer.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func NewDeployer(rpcURL *url.URL, repo *contract.ContractsRepository, senderAddr
BaseURL: rpcURL,
},
ContractsRepository: repo,
senderAddress: senderAddress,
senderAddress: senderAddress,
}, nil
}

Expand Down Expand Up @@ -80,7 +80,7 @@ func (d *Deployer) CreateContract(c *contract.CompiledContract, jsonParams []byt
args = append(args, d.senderAddress)
}

err = d.rpc.Call(&tx,"createcontract", args...)
err = d.rpc.Call(&tx, "createcontract", args...)

if err != nil {
return errors.Wrap(err, "createcontract")
Expand Down

0 comments on commit 62fbaad

Please sign in to comment.