Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EVM prototype #427

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .golangci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@ run:
- kernel/consensus
- kernel/network
- kernel/permission
- .autogen
- .compile_cache
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package evm
package address

import (
"fmt"
Expand All @@ -18,13 +18,15 @@ const (

contractNamePrefix = "1111"
contractAccountPrefix = "1112"
)

xchainAddrType = "xchain"
contractNameType = "contract-name"
contractAccountType = "contract-account"
const (
XchainAddrType = "xchain"
ContractNameType = "contract-name"
ContractAccountType = "contract-account"
)

// transfer xchain address to evm address
// XchainToEVMAddress transfer xchain address to evm address
func XchainToEVMAddress(addr string) (crypto.Address, error) {
rawAddr := base58.Decode(addr)
if len(rawAddr) < 21 {
Expand All @@ -34,11 +36,11 @@ func XchainToEVMAddress(addr string) (crypto.Address, error) {
return crypto.AddressFromBytes(ripemd160Hash)
}

// transfer evm address to xchain address
// EVMAddressToXchain transfer evm address to xchain address
func EVMAddressToXchain(evmAddress crypto.Address) (string, error) {
addrType := 1
nVersion := uint8(addrType)
bufVersion := []byte{byte(nVersion)}
bufVersion := []byte{nVersion}

outputRipemd160 := evmAddress.Bytes()

Expand All @@ -55,7 +57,7 @@ func EVMAddressToXchain(evmAddress crypto.Address) (string, error) {
return base58.Encode(slice), nil
}

// transfer contract name to evm address
// ContractNameToEVMAddress transfer contract name to evm address
func ContractNameToEVMAddress(contractName string) (crypto.Address, error) {
contractNameLength := len(contractName)
var prefixStr string
Expand All @@ -67,7 +69,7 @@ func ContractNameToEVMAddress(contractName string) (crypto.Address, error) {
return crypto.AddressFromBytes([]byte(contractName))
}

// transfer evm address to contract name
// EVMAddressToContractName transfer evm address to contract name
func EVMAddressToContractName(evmAddr crypto.Address) (string, error) {
contractNameWithPrefix := evmAddr.Bytes()
contractNameStrWithPrefix := string(contractNameWithPrefix)
Expand All @@ -78,50 +80,38 @@ func EVMAddressToContractName(evmAddr crypto.Address) (string, error) {
return contractNameStrWithPrefix[prefixIndex+1:], nil
}

// transfer contract account to evm address
// ContractAccountToEVMAddress transfer contract account to evm address
func ContractAccountToEVMAddress(contractAccount string) (crypto.Address, error) {
contractAccountValid := contractAccount[2:18]
contractAccountValid = contractAccountPrefix + contractAccountValid
return crypto.AddressFromBytes([]byte(contractAccountValid))
}

// transfer evm address to contract account
// EVMAddressToContractAccount transfer evm address to contract account
func EVMAddressToContractAccount(evmAddr crypto.Address) (string, error) {
contractNameWithPrefix := evmAddr.Bytes()
contractNameStrWithPrefix := string(contractNameWithPrefix)
return utils.GetAccountPrefix() + contractNameStrWithPrefix[4:] + "@xuper", nil
}

// 返回的合约账户不包括前缀XC和后缀@xuper(或其他链名)
// EVMAddressToContractAccountWithoutPrefixAndSuffix 返回的合约账户不包括前缀XC和后缀@xuper(或其他链名)
func EVMAddressToContractAccountWithoutPrefixAndSuffix(evmAddr crypto.Address) (string, error) {
contractNameWithPrefix := evmAddr.Bytes()
contractNameStrWithPrefix := string(contractNameWithPrefix)
return contractNameStrWithPrefix[4:], nil
}

// Deprecating, use IsContractAccount instead
func DetermineContractAccount(account string) bool {
return IsContractAccount(account)
}

// IsContractAccount returns true for a contract account
func IsContractAccount(account string) bool {
return utils.IsAccount(account) && strings.Contains(account, "@")
}

// Deprecating
// if error of incorrect name is needed, use `contract.ValidContractName`
// otherwise, use IsContractName
func DetermineContractName(contractName string) error {
return contract.ValidContractName(contractName)
}

// IsContractName determine whether it is a contract name
func IsContractName(contractName string) bool {
return contract.ValidContractName(contractName) == nil
}

// determine whether it is a contract name
// DetermineContractNameFromEVM determine whether it is a contract name
func DetermineContractNameFromEVM(evmAddr crypto.Address) (string, error) {
var addr string
var err error
Expand All @@ -141,7 +131,7 @@ func DetermineContractNameFromEVM(evmAddr crypto.Address) (string, error) {
return addr, nil
}

// determine an EVM address
// DetermineEVMAddress determine an EVM address
func DetermineEVMAddress(evmAddr crypto.Address) (string, string, error) {
evmAddrWithPrefix := evmAddr.Bytes()
evmAddrStrWithPrefix := string(evmAddrWithPrefix)
Expand All @@ -151,13 +141,13 @@ func DetermineEVMAddress(evmAddr crypto.Address) (string, string, error) {
if evmAddrStrWithPrefix[0:4] == contractAccountPrefix {
// 此时 addr 不包括前缀和后缀!
addr, err = EVMAddressToContractAccountWithoutPrefixAndSuffix(evmAddr)
addrType = contractAccountType
addrType = ContractAccountType
} else if evmAddrStrWithPrefix[0:4] == contractNamePrefix {
addr, err = EVMAddressToContractName(evmAddr)
addrType = contractNameType
addrType = ContractNameType
} else {
addr, err = EVMAddressToXchain(evmAddr)
addrType = xchainAddrType
addrType = XchainAddrType
}
if err != nil {
return "", "", err
Expand All @@ -166,20 +156,20 @@ func DetermineEVMAddress(evmAddr crypto.Address) (string, string, error) {
return addr, addrType, nil
}

// determine an xchain address
// DetermineXchainAddress determine an xchain address
func DetermineXchainAddress(xAddr string) (string, string, error) {
var addr crypto.Address
var addrType string
var err error
if IsContractAccount(xAddr) {
addr, err = ContractAccountToEVMAddress(xAddr)
addrType = contractAccountType
addrType = ContractAccountType
} else if IsContractName(xAddr) {
addr, err = ContractNameToEVMAddress(xAddr)
addrType = contractNameType
addrType = ContractNameType
} else {
addr, err = XchainToEVMAddress(xAddr)
addrType = xchainAddrType
addrType = XchainAddrType
}
if err != nil {
return "", "", err
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package evm
package address

import (
"fmt"
Expand Down Expand Up @@ -96,8 +96,8 @@ func TestDetermineEVMAddress(t *testing.T) {
if "XC"+contractAccountFromEVMAddr+"@xuper" != contractAccount {
t.Errorf("expect %s got %s", contractAccount, contractAccountFromEVMAddr)
}
if addrType != contractAccountType {
t.Errorf("expect %s got %s", contractAccountType, addrType)
if addrType != ContractAccountType {
t.Errorf("expect %s got %s", ContractAccountType, addrType)
}

// contract name
Expand All @@ -112,8 +112,8 @@ func TestDetermineEVMAddress(t *testing.T) {
if contractNameFromEVMAddr != contractName {
t.Errorf("expect %s got %s", contractName, contractNameFromEVMAddr)
}
if addrType != contractNameType {
t.Errorf("expect %s got %s", contractNameType, addrType)
if addrType != ContractNameType {
t.Errorf("expect %s got %s", ContractNameType, addrType)
}

// xchain addr
Expand All @@ -128,8 +128,8 @@ func TestDetermineEVMAddress(t *testing.T) {
if xchainFromEVMAddr != xchainAddr {
t.Errorf("expect %s got %s", xchainAddr, xchainFromEVMAddr)
}
if addrType != xchainAddrType {
t.Errorf("expect %s got %s", xchainAddrType, addrType)
if addrType != XchainAddrType {
t.Errorf("expect %s got %s", XchainAddrType, addrType)
}
}

Expand All @@ -145,8 +145,8 @@ func TestDetermineXchainAddress(t *testing.T) {
if contractAccountFromXchain != evmAddrHex {
t.Errorf("expect %s got %s", evmAddrHex, contractAccountFromXchain)
}
if addrType != contractAccountType {
t.Errorf("expect %s got %s", contractAccountType, addrType)
if addrType != ContractAccountType {
t.Errorf("expect %s got %s", ContractAccountType, addrType)
}

// contract name
Expand All @@ -160,8 +160,8 @@ func TestDetermineXchainAddress(t *testing.T) {
if contractNameFromXchain != evmAddrHex {
t.Errorf("expect %s got %s", evmAddrHex, contractNameFromXchain)
}
if addrType != contractNameType {
t.Errorf("expect %s got %s", contractNameType, addrType)
if addrType != ContractNameType {
t.Errorf("expect %s got %s", ContractNameType, addrType)
}

// xchain addr
Expand All @@ -175,8 +175,8 @@ func TestDetermineXchainAddress(t *testing.T) {
if xchainFromXchain != evmAddrHex {
t.Errorf("expect %s got %s", evmAddrHex, xchainFromXchain)
}
if addrType != xchainAddrType {
t.Errorf("expect %s got %s", xchainAddrType, addrType)
if addrType != XchainAddrType {
t.Errorf("expect %s got %s", XchainAddrType, addrType)
}
}

Expand Down
27 changes: 16 additions & 11 deletions bcs/contract/evm/creator.go → bcs/contract/evm/burrow/creator.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package evm
package burrow

import (
"encoding/hex"
"encoding/json"
"fmt"
xabi "github.com/xuperchain/xupercore/bcs/contract/evm/burrow/abi"
"github.com/xuperchain/xupercore/bcs/contract/evm/burrow/address"
"github.com/xuperchain/xupercore/lib/crypto/hash"
"math/big"
"reflect"

Expand All @@ -14,7 +17,6 @@ import (
"github.com/hyperledger/burrow/execution/evm/abi"
"github.com/hyperledger/burrow/execution/exec"

xabi "github.com/xuperchain/xupercore/bcs/contract/evm/abi"
"github.com/xuperchain/xupercore/kernel/contract"
"github.com/xuperchain/xupercore/kernel/contract/bridge"
"github.com/xuperchain/xupercore/kernel/contract/bridge/pb"
Expand Down Expand Up @@ -102,16 +104,16 @@ func (i *evmInstance) Exec() error {
}

var caller crypto.Address
if IsContractAccount(i.state.ctx.Initiator) {
caller, err = ContractAccountToEVMAddress(i.state.ctx.Initiator)
if address.IsContractAccount(i.state.ctx.Initiator) {
caller, err = address.ContractAccountToEVMAddress(i.state.ctx.Initiator)
} else {
caller, err = XchainToEVMAddress(i.state.ctx.Initiator)
caller, err = address.XchainToEVMAddress(i.state.ctx.Initiator)
}
if err != nil {
return err
}

callee, err := ContractNameToEVMAddress(i.ctx.ContractName)
callee, err := address.ContractNameToEVMAddress(i.ctx.ContractName)
if err != nil {
return err
}
Expand Down Expand Up @@ -186,7 +188,7 @@ func (i *evmInstance) Call(call *exec.CallEvent, exception *errors.Exception) er
}

func (i *evmInstance) Log(log *exec.LogEvent) error {
contractName, _, err := DetermineEVMAddress(log.Address)
contractName, _, err := address.DetermineEVMAddress(log.Address)
if err != nil {
return err
}
Expand Down Expand Up @@ -242,16 +244,16 @@ func unpackEventFromAbi(abiByte []byte, contractName string, log *exec.LogEvent)
func (i *evmInstance) deployContract() error {
var caller crypto.Address
var err error
if IsContractAccount(i.state.ctx.Initiator) {
caller, err = ContractAccountToEVMAddress(i.state.ctx.Initiator)
if address.IsContractAccount(i.state.ctx.Initiator) {
caller, err = address.ContractAccountToEVMAddress(i.state.ctx.Initiator)
} else {
caller, err = XchainToEVMAddress(i.state.ctx.Initiator)
caller, err = address.XchainToEVMAddress(i.state.ctx.Initiator)
}
if err != nil {
return err
}

callee, err := ContractNameToEVMAddress(i.ctx.ContractName)
callee, err := address.ContractNameToEVMAddress(i.ctx.ContractName)
if err != nil {
return err
}
Expand All @@ -269,6 +271,9 @@ func (i *evmInstance) deployContract() error {
return err
}
}
fmt.Printf("i.code: %+v\n", input)
fmt.Printf("i.code.length: %d\n", len(input))
fmt.Printf("hash.DoubleSha256(i.code): %s\n", hex.EncodeToString(hash.UsingRipemd160(input)))

params := engine.CallParams{
CallType: exec.CallTypeCode,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package evm
package burrow

import (
"encoding/hex"
Expand Down
Loading