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

api: Improve logging and fix successful proxy execution check #1605

Merged
merged 12 commits into from
Dec 13, 2023
3 changes: 3 additions & 0 deletions bootstrap/bootstrappers/integration_test/test_bootstrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package integration_test
import (
"context"
"fmt"
"math/rand"
"os"
"os/exec"
"time"
Expand All @@ -21,6 +22,8 @@ var (
type Bootstrapper struct{}

func (b *Bootstrapper) TestBootstrap(_ map[string]any) error {
rand.Seed(time.Now().Unix())

if err := os.Chdir(path.ProjectRoot); err != nil {
log.Errorf("Couldn't change path to project root: %s", err)

Expand Down
2 changes: 1 addition & 1 deletion build/centrifuge-chain
Submodule centrifuge-chain updated 617 files
113 changes: 42 additions & 71 deletions build/scripts/run_centrifuge_chain.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
# Multiple coroutines might execute this script concurrently, the following acts as a lock.
[ "${FLOCKER}" != "$0" ] && exec env FLOCKER="$0" flock -e "$0" "$0" "$@"


CENT_CHAIN_DOCKER_START_TIMEOUT=${CENT_CHAIN_DOCKER_START_TIMEOUT:-600}
CENT_CHAIN_DOCKER_START_INTERVAL=${CENT_CHAIN_DOCKER_START_INTERVAL:-2}

Expand All @@ -17,92 +16,64 @@ else
echo "Container ${CC_DOCKER_CONTAINER_NAME} is not currently running. Going to start."
fi

function wait_for_container() {
container_name=$1
if [ "$container_name" == "" ]; then
echo "Please provide a docker container name."
exit 1
fi

echo "Waiting for docker container '$container_name' to start up..."

maxCount=$(( CENT_CHAIN_DOCKER_START_TIMEOUT / CENT_CHAIN_DOCKER_START_INTERVAL ))
echo "MaxCount: $maxCount"

count=0
while true
do
validating=$(docker logs "$container_name" 2>&1 | grep 'finalized #')
if [ "$validating" != "" ]; then
echo "Container '$container_name' successfully started"
break
elif [ $count -ge $maxCount ]; then
echo "Timeout reached while waiting for container '$container_name'"
exit 1
fi
sleep "$CENT_CHAIN_DOCKER_START_INTERVAL";
((count++))
done
}

cc_docker_image_tag="${PARA_DOCKER_IMAGE_TAG:-latest}"
parachain_spec="${PARA_CHAIN_SPEC:-development-local}"

export PARA_DOCKER_IMAGE_TAG=$cc_docker_image_tag
export CC_DOCKER_TAG=$cc_docker_image_tag
export PARA_CHAIN_SPEC=$parachain_spec

# Setup
PARENT_DIR=$(pwd)

mkdir -p /tmp/centrifuge-pod/deps/res
cp "${PARENT_DIR}"/build/centrifuge-chain/docker-compose-local-relay.yml /tmp/centrifuge-pod/deps/
cp "${PARENT_DIR}"/build/centrifuge-chain/docker-compose-local-chain.yml /tmp/centrifuge-pod/deps/
cp "${PARENT_DIR}"/build/centrifuge-chain/res/rococo-local.json /tmp/centrifuge-pod/deps/res/
docker network inspect docker_default
if [ $? -ne 0 ]; then
docker network create docker_default
fi
cd "${PARENT_DIR}"/build/centrifuge-chain/ || exit

################## Run RelayChain #########################
cd "${PARENT_DIR}"/build/centrifuge-chain || exit
## Tweaking network
default_network=$(cat /tmp/centrifuge-pod/deps/docker-compose-local-relay.yml | grep "name: docker_default")
if [[ $default_network == "" ]]; then
cat <<EOT >> /tmp/centrifuge-pod/deps/docker-compose-local-relay.yml
networks:
default:
external:
name: docker_default
EOT
fi
"${PARENT_DIR}"/build/centrifuge-chain/scripts/init.sh start-relay-chain

docker-compose -f /tmp/centrifuge-pod/deps/docker-compose-local-relay.yml up -d

echo "Waiting for Relay Chain to Start Up ..."
maxCount=$(( CENT_CHAIN_DOCKER_START_TIMEOUT / CENT_CHAIN_DOCKER_START_INTERVAL ))
echo "MaxCount: $maxCount"
count=0
while true
do
validating=$(docker logs alice 2>&1 | grep 'finalized #')
if [ "$validating" != "" ]; then
echo "RelayChain successfully started"
break
elif [ $count -ge $maxCount ]; then
echo "Timeout Starting out RelayChain"
exit 1
fi
sleep "$CENT_CHAIN_DOCKER_START_INTERVAL";
((count++))
done
wait_for_container "alice"

################## Run CentChain #########################
## Centrifuge Chain local Development testnet
## Tweaking network
default_network=$(cat /tmp/centrifuge-pod/deps/docker-compose-local-chain.yml | grep "name: docker_default")
if [[ $default_network == "" ]]; then
cat <<EOT >> /tmp/centrifuge-pod/deps/docker-compose-local-chain.yml
networks:
default:
external:
name: docker_default
EOT
fi
"${PARENT_DIR}"/build/centrifuge-chain/scripts/init.sh start-parachain-docker

PARA_CHAIN_SPEC=development-local \
docker-compose -f /tmp/centrifuge-pod/deps/docker-compose-local-chain.yml up -d

echo "Waiting for Centrifuge Chain to Start Up ..."
maxCount=$(( CENT_CHAIN_DOCKER_START_TIMEOUT / CENT_CHAIN_DOCKER_START_INTERVAL ))
echo "MaxCount: $maxCount"
count=0
while true
do
validating=$(docker logs cc-alice 2>&1 | grep 'finalized #')
if [ "$validating" != "" ]; then
echo "CentChain successfully started"
break
elif [ $count -ge $maxCount ]; then
echo "Timeout Starting out CentChain"
exit 1
fi
sleep "$CENT_CHAIN_DOCKER_START_INTERVAL";
((count++))
done
wait_for_container "cc-alice"

################## Onboard ###############################

echo "sourcing in nvm"
. $NVM_DIR/nvm.sh
nvm use v17

echo "Onboarding Centrifuge Parachain ..."
DOCKER_ONBOARD=true \
PARA_CHAIN_SPEC=development-local \
./scripts/init.sh onboard-parachain

echo "Note that the Centrifuge Chain will start producing blocks when onboarding is complete"
13 changes: 9 additions & 4 deletions centchain/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -313,11 +313,15 @@ func (a *api) SubmitAndWatch(

txHash, bn, sig, err := a.SubmitExtrinsic(ctx, meta, c, krp)
if err != nil {
log.Errorf("Extrinsic submission error - %s", err)

return info, ErrExtrinsicSubmission
}

s, err := getSignature(sig)
if err != nil {
log.Errorf("Signature retrieval error - %s", err)

return info, err
}

Expand All @@ -328,6 +332,8 @@ func (a *api) SubmitAndWatch(
job := gocelery.NewRunnerFuncJob("", task, nil, nil, time.Time{})
res, err := a.dispatcher.Dispatch(identity, job)
if err != nil {
log.Errorf("Dispatcher error - %s", err)

return info, fmt.Errorf("failed to dispatch job: %w", err)
}

Expand Down Expand Up @@ -518,9 +524,8 @@ func getErrorIDFromDispatchError(value any) (*registry.ErrorID, error) {
}

const (
ProxyExecutedEventName = "Proxy.ProxyExecuted"
ResultFieldName = "Result.result"
ProxyExecutedExpectedLookupIndex = 40
ProxyExecutedEventName = "Proxy.ProxyExecuted"
ResultFieldName = "Result.result"
)

func checkSuccessfulProxyExecution(meta *types.Metadata, events []*parser.Event, extrinsicIdx int) error {
Expand All @@ -538,7 +543,7 @@ func checkSuccessfulProxyExecution(meta *types.Metadata, events []*parser.Event,
return errors.New("result field has unexpected size")
}

if res[0].Value == nil && res[0].LookupIndex == ProxyExecutedExpectedLookupIndex {
if res[0].Value == nil {
// The DispatchResult is Ok(()).
return nil
}
Expand Down
3 changes: 1 addition & 2 deletions centchain/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1106,8 +1106,7 @@ func TestApi_checkExtrinsicEventSuccess_ExtrinsicSuccess_WithProxySuccess(t *tes
Name: ResultFieldName,
Value: registry.DecodedFields{
{
Value: nil,
LookupIndex: ProxyExecutedExpectedLookupIndex,
Value: nil,
},
},
LookupIndex: 0,
Expand Down
4 changes: 4 additions & 0 deletions pallets/loans/api_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ func TestIntegration_CreatedLoanRetrieval(t *testing.T) {
},
types.NewU128(*big.NewInt(rand.Int63())),
[]byte("test"),
[]pallets.WriteOffRule{},
)

// Assign the Borrower permission to Alice's account.
Expand Down Expand Up @@ -191,6 +192,9 @@ func TestIntegration_CreatedLoanRetrieval(t *testing.T) {

loanCreateCall := pallets.GetCreateLoanCallCreationFn(poolID, loanInfo)

_, _, _ = addBorrowerPermissionsCall, loanCreateCall, registerPoolCall
_, _ = nftCollectionCall, nftMintCall

// Execute the batch call using the test keyring.

ctx, cancel := context.WithTimeout(context.Background(), 3*time.Minute)
Expand Down
8 changes: 5 additions & 3 deletions pallets/permissions/api_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ import (
"time"

"github.com/centrifuge/go-substrate-rpc-client/v4/types"
"github.com/centrifuge/pod/pallets/utility"
"github.com/centrifuge/pod/testingutils/keyrings"
"github.com/stretchr/testify/assert"

"github.com/centrifuge/pod/bootstrap"
"github.com/centrifuge/pod/bootstrap/bootstrappers/integration_test"
"github.com/centrifuge/pod/bootstrap/bootstrappers/testlogging"
Expand All @@ -22,11 +26,8 @@ import (
"github.com/centrifuge/pod/jobs"
"github.com/centrifuge/pod/pallets"
"github.com/centrifuge/pod/pallets/permissions"
"github.com/centrifuge/pod/pallets/utility"
"github.com/centrifuge/pod/storage/leveldb"
genericUtils "github.com/centrifuge/pod/testingutils/generic"
"github.com/centrifuge/pod/testingutils/keyrings"
"github.com/stretchr/testify/assert"
)

var integrationTestBootstrappers = []bootstrap.TestBootstrapper{
Expand Down Expand Up @@ -105,6 +106,7 @@ func TestIntegration_PermissionRolesRetrieval(t *testing.T) {
},
types.NewU128(*big.NewInt(rand.Int63())),
[]byte("test"),
[]pallets.WriteOffRule{},
)

// Assign the Borrower permission to Alice's account.
Expand Down
39 changes: 39 additions & 0 deletions pallets/test_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -527,6 +527,43 @@ type StakingCurrency struct {
IsBlockRewards bool
}

type WriteOffTrigger struct {
IsPrincipalOverdue bool
AsPrincipalOverdue types.U64

IsPriceOutdated bool
AsPriceOutdated types.U64
}

func (w WriteOffTrigger) Encode(encoder scale.Encoder) error {
switch {
case w.IsPrincipalOverdue:
if err := encoder.PushByte(0); err != nil {
return err
}

return encoder.Encode(w.AsPrincipalOverdue)
case w.IsPriceOutdated:
if err := encoder.PushByte(1); err != nil {
return err
}

return encoder.Encode(w.AsPriceOutdated)
default:
return fmt.Errorf("unsupported writeoff trigger")
}
}

type WriteOffStatus struct {
Percentage types.U128
Penalty types.U128
}

type WriteOffRule struct {
Triggers []WriteOffTrigger
Status WriteOffStatus
}

const (
registerPoolCall = "PoolRegistry.register"
)
Expand All @@ -538,6 +575,7 @@ func GetRegisterPoolCallCreationFn(
currency CurrencyID,
maxReserve types.U128,
metadata []byte,
writeOffPolicy []WriteOffRule,
) centchain.CallProviderFn {
return func(meta *types.Metadata) (*types.Call, error) {
call, err := types.NewCall(
Expand All @@ -549,6 +587,7 @@ func GetRegisterPoolCallCreationFn(
currency,
maxReserve,
types.NewOption(metadata),
writeOffPolicy,
)

if err != nil {
Expand Down
1 change: 1 addition & 0 deletions testworld/investor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ func TestInvestorAPI_GetAsset(t *testing.T) {
},
types.NewU128(*big.NewInt(rand.Int63())),
[]byte("test"),
[]pallets.WriteOffRule{},
)

// Assign the Borrower permission to the main account on the Alice host.
Expand Down
Loading