Skip to content

Commit

Permalink
Add return value to SendPayloadWithTransactionBuilder
Browse files Browse the repository at this point in the history
  • Loading branch information
muXxer committed Oct 9, 2023
1 parent 4e43c6d commit 66d9ef2
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 12 deletions.
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ require (
github.com/h2non/parth v0.0.0-20190131123155-b4df798d6542 // indirect
github.com/holiman/uint256 v1.2.3 // indirect
github.com/iancoleman/orderedmap v0.3.0 // indirect
github.com/iotaledger/hive.go/codegen v0.0.0-20231005142627-86973b2edb3b // indirect
github.com/kr/text v0.2.0 // indirect
github.com/mr-tron/base58 v1.2.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ github.com/holiman/uint256 v1.2.3 h1:K8UWO1HUJpRMXBxbmaY1Y8IAMZC/RsKB+ArEnnK4l5o
github.com/holiman/uint256 v1.2.3/go.mod h1:SC8Ryt4n+UBbPbIBKaG9zbbDlp4jOru9xFZmPzLUTxw=
github.com/iancoleman/orderedmap v0.3.0 h1:5cbR2grmZR/DiVt+VJopEhtVs9YGInGIxAoMJn+Ichc=
github.com/iancoleman/orderedmap v0.3.0/go.mod h1:XuLcCUkdL5owUCQeF2Ue9uuw1EptkJDkXXS7VoV7XGE=
github.com/iotaledger/hive.go/codegen v0.0.0-20231005142627-86973b2edb3b h1:RpCqfmmxrkTdxPm56UzF1QuHppTo3VtyzhfaUUfzVA0=
github.com/iotaledger/hive.go/codegen v0.0.0-20231005142627-86973b2edb3b/go.mod h1:zBiQaY4DL7xx/gNgb1MWqMaV62x2o937nBCEaW++ztI=
github.com/iotaledger/hive.go/constraints v0.0.0-20231005142627-86973b2edb3b h1:8FdKoB755PjCL1aHTi+2rPt9lCUS4B2wg2fNKykw5dc=
github.com/iotaledger/hive.go/constraints v0.0.0-20231005142627-86973b2edb3b/go.mod h1:dOBOM2s4se3HcWefPe8sQLUalGXJ8yVXw58oK8jke3s=
github.com/iotaledger/hive.go/core v1.0.0-rc.3.0.20231005142627-86973b2edb3b h1:gQ+Wqg8h/LRpgX3CsmaOZYdMIBVL4fAPIrZbmA6Wn3Q=
Expand Down
23 changes: 14 additions & 9 deletions nodeclient/blockissuer_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ type (
// SendPayload sends a BlockPayload to the block issuer.
SendPayload(ctx context.Context, payload iotago.BlockPayload, commitmentID iotago.CommitmentID, numPoWWorkers ...int) (*apimodels.BlockCreatedResponse, error)
// SendPayloadWithTransactionBuilder automatically allots the needed mana and sends a BlockPayload to the block issuer.
SendPayloadWithTransactionBuilder(ctx context.Context, builder *builder.TransactionBuilder, signer iotago.AddressSigner, storedManaOutputIndex int, numPoWWorkers ...int) (*apimodels.BlockCreatedResponse, error)
SendPayloadWithTransactionBuilder(ctx context.Context, builder *builder.TransactionBuilder, signer iotago.AddressSigner, storedManaOutputIndex int, numPoWWorkers ...int) (iotago.BlockPayload, *apimodels.BlockCreatedResponse, error)
}

blockIssuerClient struct {
Expand Down Expand Up @@ -112,31 +112,31 @@ func (client *blockIssuerClient) SendPayload(ctx context.Context, payload iotago
return client.mineNonceAndSendPayload(ctx, payload, commitmentID, blockIssuerInfo.PowTargetTrailingZeros, numPoWWorkers...)
}

func (client *blockIssuerClient) SendPayloadWithTransactionBuilder(ctx context.Context, builder *builder.TransactionBuilder, signer iotago.AddressSigner, storedManaOutputIndex int, numPoWWorkers ...int) (*apimodels.BlockCreatedResponse, error) {
func (client *blockIssuerClient) SendPayloadWithTransactionBuilder(ctx context.Context, builder *builder.TransactionBuilder, signer iotago.AddressSigner, storedManaOutputIndex int, numPoWWorkers ...int) (iotago.BlockPayload, *apimodels.BlockCreatedResponse, error) {
// get the info from the block issuer
blockIssuerInfo, err := client.Info(ctx)
if err != nil {
return nil, ierrors.Wrap(err, "failed to get the block issuer info")
return nil, nil, ierrors.Wrap(err, "failed to get the block issuer info")
}

// parse the block issuer address
//nolint:contextcheck // false positive
_, blockIssuerAddress, err := iotago.ParseBech32(blockIssuerInfo.BlockIssuerAddress)
if err != nil {
return nil, ierrors.Wrap(err, "failed to parse the block issuer address")
return nil, nil, ierrors.Wrap(err, "failed to parse the block issuer address")
}

// check if the block issuer address is an account address
blockIssuerAccountAddress, isAccount := blockIssuerAddress.(*iotago.AccountAddress)
if !isAccount {
return nil, ierrors.New("failed to parse the block issuer address")
return nil, nil, ierrors.New("failed to parse the block issuer address")
}

// get the current commitmentID and reference mana cost to calculate
// the correct value for the mana that needs to be alloted to the block issuer.
blockIssuance, err := client.core.BlockIssuance(ctx)
if err != nil {
return nil, ierrors.Wrap(err, "failed to get the latest block issuance infos")
return nil, nil, ierrors.Wrap(err, "failed to get the latest block issuance infos")
}

// allot the required mana to the block issuer
Expand All @@ -145,14 +145,19 @@ func (client *blockIssuerClient) SendPayloadWithTransactionBuilder(ctx context.C
// sign the transaction
payload, err := builder.Build(signer)
if err != nil {
return nil, ierrors.Wrap(err, "failed to build the signed transaction payload")
return nil, nil, ierrors.Wrap(err, "failed to build the signed transaction payload")
}

//nolint:contextcheck // false positive
commitmentID, err := blockIssuance.Commitment.ID()
if err != nil {
return nil, ierrors.Wrap(err, "failed to calculate the commitment ID")
return nil, nil, ierrors.Wrap(err, "failed to calculate the commitment ID")
}

return client.mineNonceAndSendPayload(ctx, payload, commitmentID, blockIssuerInfo.PowTargetTrailingZeros, numPoWWorkers...)
blockCreatedResponse, err := client.mineNonceAndSendPayload(ctx, payload, commitmentID, blockIssuerInfo.PowTargetTrailingZeros, numPoWWorkers...)
if err != nil {
return nil, nil, err
}

return payload, blockCreatedResponse, nil
}

0 comments on commit 66d9ef2

Please sign in to comment.