Skip to content

Commit

Permalink
Merge pull request #362 from aergoio/topic/hardfork-v4
Browse files Browse the repository at this point in the history
Hardfork 4
  • Loading branch information
kroggen authored Oct 15, 2024
2 parents fbd352b + 033aa86 commit 9ef7010
Show file tree
Hide file tree
Showing 81 changed files with 9,506 additions and 1,760 deletions.
1 change: 0 additions & 1 deletion Docker/Dockerfile.builder
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,3 @@ RUN apt-get -y update && apt-get -y install build-essential git cmake binutils m
RUN git clone --branch ${GIT_TAG} --recursive https://github.com/aergoio/aergo.git \
&& cd aergo \
&& make aergosvr polaris colaris aergocli aergoluac brick

4 changes: 4 additions & 0 deletions Docker/Dockerfile.local
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
FROM golang:1.19.0-bullseye as builder
RUN apt-get -y update && apt-get -y install build-essential git cmake binutils m4 file
COPY . aergo
RUN cd aergo && make aergosvr polaris colaris aergocli aergoluac brick
92 changes: 92 additions & 0 deletions Docker/build-local.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
#!/usr/bin/env bash

# This script can be used to build the Docker images manually (outside of CI)

set -e

MAIN_TAG=$1
SECOND_TAG=$2
THIRD_TAG=$3

if [[ -z "$MAIN_TAG" ]]
then
echo "Usage:"
echo " build-local.sh tag [second-tag] [third-tag]"
echo "Example:"
echo " build-local.sh 0.12.0-rc"
echo " build-local.sh 0.12.0 0.12 latest"
exit 1
fi


echo "Preparing local folder for build"

cd ..
git submodule update --init --recursive
make clean || true
rm -rf build
go clean --cache


if [[ -z "$THIRD_TAG" ]]
then
if [[ -z "$SECOND_TAG" ]]
then
declare -a tags=("$MAIN_TAG")
else
declare -a tags=("$MAIN_TAG" "$SECOND_TAG")
fi
else
declare -a tags=("$MAIN_TAG" "$SECOND_TAG" "$THIRD_TAG")
fi

echo "Building Docker images for ${tags[*]} using local folder"
sleep 1

BUILDER_TAG="aergo/local-builder"
echo "Building ${BUILDER_TAG}"

docker build --no-cache --file Docker/Dockerfile.local -t ${BUILDER_TAG} .
cd -
docker create --name extract ${BUILDER_TAG}
docker cp extract:/go/aergo/bin/ .
docker cp extract:/go/aergo/cmd/brick/arglog.toml bin/brick-arglog.toml
docker cp extract:/go/aergo/libtool/lib/ .
docker rm -f extract

declare -a names=("node" "tools" "polaris")
for name in "${names[@]}"
do
tagsExpanded=()
for tag in "${tags[@]}"; do
tagsExpanded+=("-t aergo/$name:$tag")
done
echo "[aergo/$name:${tags[*]}]"
DOCKERFILE="Dockerfile.$name"
echo docker build -q ${tagsExpanded[@]} --file $DOCKERFILE .
imageid=`docker build -q ${tagsExpanded[@]} --file $DOCKERFILE .`
docker images --format "Done: \t{{.Repository}}:{{.Tag}} \t{{.ID}} ({{.Size}})" | grep "${imageid:7:12}"
done

rm -rf bin lib

echo -e "\nREPOSITORY TAG IMAGE ID CREATED SIZE"
for name in "${names[@]}"
do
for tag in "${tags[@]}"
do
docker images aergo/$name:$tag | tail -1
done
done

echo -e "\nYou can now push these to Docker Hub."
echo "For example:"

declare -a names=("node" "tools" "polaris")
for name in "${names[@]}"
do
for tag in "${tags[@]}"
do
echo " docker push aergo/$name:$tag"
done
done
2 changes: 1 addition & 1 deletion aergo-protobuf
16 changes: 12 additions & 4 deletions chain/chainhandle.go
Original file line number Diff line number Diff line change
Expand Up @@ -979,12 +979,19 @@ func executeTx(execCtx context.Context, ccc consensus.ChainConsensusCluster, cdb
return err
}

if recipient, err = name.Resolve(bs, txBody.Recipient, isQuirkTx); err != nil {
return err
isMultiCall := (txBody.Type == types.TxType_MULTICALL)

if !isMultiCall {
if recipient, err = name.Resolve(bs, txBody.Recipient, isQuirkTx); err != nil {
return err
}
}

var receiver *state.AccountState
status := "SUCCESS"
if len(recipient) > 0 {
if isMultiCall {
receiver = sender
} else if len(recipient) > 0 {
receiver, err = state.GetAccountState(recipient, bs.StateDB)
if receiver != nil && txBody.Type == types.TxType_REDEPLOY {
status = "RECREATED"
Expand All @@ -1001,8 +1008,9 @@ func executeTx(execCtx context.Context, ccc consensus.ChainConsensusCluster, cdb
var txFee *big.Int
var rv string
var events []*types.Event

switch txBody.Type {
case types.TxType_NORMAL, types.TxType_REDEPLOY, types.TxType_TRANSFER, types.TxType_CALL, types.TxType_DEPLOY:
case types.TxType_NORMAL, types.TxType_TRANSFER, types.TxType_CALL, types.TxType_MULTICALL, types.TxType_DEPLOY, types.TxType_REDEPLOY:
rv, events, txFee, err = contract.Execute(execCtx, bs, cdb, tx.GetTx(), sender, receiver, bi, executionMode, false)
sender.SubBalance(txFee)
case types.TxType_GOVERNANCE:
Expand Down
126 changes: 86 additions & 40 deletions cmd/aergocli/cmd/contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cmd
import (
"bytes"
"context"
"math/big"
"encoding/json"
"errors"
"fmt"
Expand Down Expand Up @@ -70,17 +71,17 @@ func init() {
contractCmd.PersistentFlags().Uint64VarP(&gas, "gaslimit", "g", 0, "Gas limit")

deployCmd := &cobra.Command{
Use: `deploy [flags] --payload 'payload string' <creatorAddress> [args]
aergocli contract deploy [flags] <creatorAddress> <bcfile> <abifile> [args]
Use: `deploy [flags] <creatorAddress> <path-to-lua-file> [args]
aergocli contract deploy [flags] <creatorAddress> --payload 'payload string' [args]
You can pass constructor arguments by passing a JSON string as the optional final parameter, e.g. "[1, 2, 3]".`,
Short: "Deploy a compiled contract to the server",
Args: nArgs([]int{1, 2, 3, 4}),
You can pass arguments to the constructor() function by passing a JSON string as the optional final parameter, e.g. '[1, "test"]'`,
Short: "Deploy a contract to the server",
Args: nArgs([]int{1, 2, 3}),
RunE: runDeployCmd,
DisableFlagsInUseLine: true,
}
deployCmd.PersistentFlags().Uint64Var(&nonce, "nonce", 0, "manually set a nonce (default: set nonce automatically)")
deployCmd.PersistentFlags().StringVar(&data, "payload", "", "result of compiling a contract")
deployCmd.PersistentFlags().StringVar(&data, "payload", "", "result of compiling a contract with aergoluac")
deployCmd.PersistentFlags().StringVar(&amount, "amount", "0", "amount of token to send with deployment, in aer")
deployCmd.PersistentFlags().StringVarP(&contractID, "redeploy", "r", "", "redeploy the contract")
deployCmd.Flags().StringVar(&pw, "password", "", "password (optional, will be asked on the terminal if not given)")
Expand All @@ -101,6 +102,19 @@ func init() {
callCmd.PersistentFlags().BoolVar(&feeDelegation, "delegation", false, "request fee delegation to contract")
callCmd.Flags().StringVar(&pw, "password", "", "password (optional, will be asked on the terminal if not given)")

multicallCmd := &cobra.Command{
Use: `multicall [flags] <sender> <script>
The script is a JSON array of arrays, enclosed as a string, containing the commands`,
Short: "Calls multiple contracts / functions",
Args: nArgs([]int{2}),
RunE: runMulticallCmd,
}
multicallCmd.PersistentFlags().Uint64Var(&nonce, "nonce", 0, "manually set a nonce (default: set nonce automatically)")
multicallCmd.PersistentFlags().StringVar(&chainIdHash, "chainidhash", "", "chain id hash value encoded by base58")
multicallCmd.PersistentFlags().BoolVar(&toJSON, "tojson", false, "display json transaction instead of sending to blockchain")
multicallCmd.Flags().StringVar(&pw, "password", "", "password (optional, will be asked on the terminal if not given)")

stateQueryCmd := &cobra.Command{
Use: "statequery [flags] <contractAddress> <varname> [varindex]",
Short: "query the state of a contract with variable name and optional index",
Expand All @@ -113,6 +127,7 @@ func init() {
contractCmd.AddCommand(
deployCmd,
callCmd,
multicallCmd,
&cobra.Command{
Use: "abi [flags] <contractAddress>",
Short: "Get ABI of the contract",
Expand All @@ -130,20 +145,6 @@ func init() {
rootCmd.AddCommand(contractCmd)
}

func isHexString(s string) bool {
// check is the input has even number of characters
if len(s)%2 != 0 {
return false
}
// check if the input contains only hex characters
for _, c := range s {
if !((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F')) {
return false
}
}
return true
}

func runDeployCmd(cmd *cobra.Command, args []string) error {
var err error
var code []byte
Expand All @@ -164,31 +165,39 @@ func runDeployCmd(cmd *cobra.Command, args []string) error {
nonce = state.GetNonce() + 1
}

chainInfo, err := client.GetChainInfo(context.Background(), &types.Empty{})
if err != nil {
return fmt.Errorf("could not retrieve chain info: %v", err.Error())
}

var payload []byte
if len(data) == 0 {
if len(args) < 3 {
if chainInfo.Id.Version < 4 {
cmd.SilenceUsage = false
return errors.New("for old hardforks use aergoluac and --payload method instead")
}
if len(args) < 2 {
cmd.SilenceUsage = false
return errors.New("not enough arguments")
}
code, err = os.ReadFile(args[1])
if err != nil {
return fmt.Errorf("failed to read code file: %v", err.Error())
}
var abi []byte
abi, err = os.ReadFile(args[2])
if err != nil {
return fmt.Errorf("failed to read abi file: %v", err.Error())
}
if len(args) == 4 {
if len(args) == 3 {
var ci types.CallInfo
err = json.Unmarshal([]byte(args[3]), &ci.Args)
err = json.Unmarshal([]byte(args[2]), &ci.Args)
if err != nil {
return fmt.Errorf("failed to parse JSON: %v", err.Error())
return fmt.Errorf("failed to parse arguments (JSON): %v", err.Error())
}
deployArgs = []byte(args[3])
deployArgs = []byte(args[2])
}
payload = luac.NewLuaCodePayload(luac.NewLuaCode(code, abi), deployArgs)
payload = luac.NewLuaCodePayload(luac.LuaCode(code), deployArgs)
} else {
if chainInfo.Id.Version >= 4 {
cmd.SilenceUsage = false
return errors.New("this chain only accepts deploy in plain source code\nuse the other method instead")
}
if len(args) == 2 {
var ci types.CallInfo
err = json.Unmarshal([]byte(args[1]), &ci.Args)
Expand All @@ -198,7 +207,11 @@ func runDeployCmd(cmd *cobra.Command, args []string) error {
deployArgs = []byte(args[1])
}
// check if the data is in hex format
if isHexString(data) {
if hex.IsHexString(data) {
if deployArgs != nil {
cmd.SilenceUsage = false
return errors.New("the call arguments are expected to be already on the hex data")
}
// the data is expected to be copied from aergoscan view of
// the transaction that deployed the contract
payload, err = hex.Decode(data)
Expand Down Expand Up @@ -249,13 +262,7 @@ func runCallCmd(cmd *cobra.Command, args []string) error {
if err != nil {
return fmt.Errorf("could not decode sender address: %v", err.Error())
}
if nonce == 0 {
state, err := client.GetState(context.Background(), &types.SingleBytes{Value: caller})
if err != nil {
return fmt.Errorf("failed to get creator account's state: %v", err.Error())
}
nonce = state.GetNonce() + 1
}

contract, err := types.DecodeAddress(args[1])
if err != nil {
return fmt.Errorf("could not decode contract address: %v", err.Error())
Expand Down Expand Up @@ -310,6 +317,45 @@ func runCallCmd(cmd *cobra.Command, args []string) error {
},
}

return sendCallTx(cmd, tx, caller)
}

func runMulticallCmd(cmd *cobra.Command, args []string) error {
cmd.SilenceUsage = true

caller, err := types.DecodeAddress(args[0])
if err != nil {
return fmt.Errorf("could not decode sender address: %v", err.Error())
}

script := args[1]

tx := &types.Tx{
Body: &types.TxBody{
Nonce: nonce,
Account: caller,
Recipient: []byte{},
Payload: []byte(script),
Amount: big.NewInt(0).Bytes(),
GasLimit: gas,
Type: types.TxType_MULTICALL,
},
}

return sendCallTx(cmd, tx, caller)
}

func sendCallTx(cmd *cobra.Command, tx *types.Tx, sender []byte) error {
var err error

if tx.GetBody().GetNonce() == 0 {
state, err := client.GetState(context.Background(), &types.SingleBytes{Value: sender})
if err != nil {
return fmt.Errorf("failed to get sender account's state: %v", err.Error())
}
tx.GetBody().Nonce = state.GetNonce() + 1
}

if chainIdHash != "" {
rawCidHash, err := base58.Decode(chainIdHash)
if err != nil {
Expand All @@ -330,7 +376,7 @@ func runCallCmd(cmd *cobra.Command, args []string) error {
}

if rootConfig.KeyStorePath != "" {
if errStr := fillSign(tx, rootConfig.KeyStorePath, pw, caller); errStr != "" {
if errStr := fillSign(tx, rootConfig.KeyStorePath, pw, sender); errStr != "" {
return errors.New(errStr)
}
} else {
Expand Down
Loading

0 comments on commit 9ef7010

Please sign in to comment.