-
Notifications
You must be signed in to change notification settings - Fork 113
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1312 from iotaledger/develop
Merge v0.6.0 to master
- Loading branch information
Showing
187 changed files
with
17,960 additions
and
2,072 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,78 @@ | ||
package client | ||
|
||
import ( | ||
"context" | ||
"crypto" | ||
"net/http" | ||
|
||
"github.com/cockroachdb/errors" | ||
"github.com/iotaledger/hive.go/identity" | ||
"github.com/mr-tron/base58" | ||
|
||
"github.com/iotaledger/goshimmer/packages/ledgerstate" | ||
"github.com/iotaledger/goshimmer/packages/mana" | ||
"github.com/iotaledger/goshimmer/packages/pow" | ||
"github.com/iotaledger/goshimmer/plugins/faucet" | ||
"github.com/iotaledger/goshimmer/plugins/webapi/jsonmodels" | ||
) | ||
|
||
const ( | ||
routeFaucet = "faucet" | ||
) | ||
|
||
var ( | ||
defaultPOWTarget = 25 | ||
powWorker = pow.New(crypto.BLAKE2b_512, 1) | ||
) | ||
|
||
// SendFaucetRequest requests funds from faucet nodes by sending a faucet request payload message. | ||
func (api *GoShimmerAPI) SendFaucetRequest(base58EncodedAddr string, pledgeIDs ...string) (*jsonmodels.FaucetResponse, error) { | ||
aManaPledgeID, cManaPledgeID := "", "" | ||
func (api *GoShimmerAPI) SendFaucetRequest(base58EncodedAddr string, powTarget int, pledgeIDs ...string) (*jsonmodels.FaucetResponse, error) { | ||
var aManaPledgeID identity.ID | ||
var cManaPledgeID identity.ID | ||
if len(pledgeIDs) > 1 { | ||
aManaPledgeID, cManaPledgeID = pledgeIDs[0], pledgeIDs[1] | ||
aManaPledgeIDFromString, err := mana.IDFromStr(pledgeIDs[0]) | ||
if err != nil { | ||
aManaPledgeID = aManaPledgeIDFromString | ||
} | ||
cManaPledgeIDFromString, err := mana.IDFromStr(pledgeIDs[1]) | ||
if err != nil { | ||
cManaPledgeID = cManaPledgeIDFromString | ||
} | ||
} | ||
|
||
address, err := ledgerstate.AddressFromBase58EncodedString(base58EncodedAddr) | ||
if err != nil { | ||
return nil, errors.Errorf("could not decode address from string: %w", err) | ||
} | ||
|
||
nonce, err := computeFaucetPoW(address, aManaPledgeID, cManaPledgeID, powTarget) | ||
if err != nil { | ||
return nil, errors.Errorf("could not compute faucet PoW: %w", err) | ||
} | ||
|
||
res := &jsonmodels.FaucetResponse{} | ||
if err := api.do(http.MethodPost, routeFaucet, | ||
&jsonmodels.FaucetRequest{Address: base58EncodedAddr, AccessManaPledgeID: aManaPledgeID, ConsensusManaPledgeID: cManaPledgeID}, res); err != nil { | ||
&jsonmodels.FaucetRequest{ | ||
Address: base58EncodedAddr, | ||
AccessManaPledgeID: base58.Encode(aManaPledgeID.Bytes()), | ||
ConsensusManaPledgeID: base58.Encode(cManaPledgeID.Bytes()), | ||
Nonce: nonce, | ||
}, res); err != nil { | ||
return nil, err | ||
} | ||
|
||
return res, nil | ||
} | ||
|
||
func computeFaucetPoW(address ledgerstate.Address, aManaPledgeID, cManaPledgeID identity.ID, powTarget int) (nonce uint64, err error) { | ||
if powTarget < 0 { | ||
powTarget = defaultPOWTarget | ||
} | ||
|
||
faucetRequest := faucet.NewRequest(address, aManaPledgeID, cManaPledgeID, 0) | ||
|
||
objectBytes := faucetRequest.Bytes() | ||
powRelevantBytes := objectBytes[:len(objectBytes)-pow.NonceBytes] | ||
|
||
return powWorker.Mine(context.Background(), powRelevantBytes, powTarget) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,192 @@ | ||
package client | ||
|
||
import ( | ||
"net/http" | ||
"strings" | ||
|
||
json_models "github.com/iotaledger/goshimmer/plugins/webapi/jsonmodels" | ||
) | ||
|
||
const ( | ||
// basic routes | ||
routeGetAddresses = "ledgerstate/addresses/" | ||
routeGetBranches = "ledgerstate/branches/" | ||
routeGetOutputs = "ledgerstate/outputs/" | ||
routeGetTransactions = "ledgerstate/transactions/" | ||
routePostTransactions = "ledgerstate/transactions" | ||
|
||
// route path modifiers | ||
pathUnspentOutputs = "/unspentOutputs" | ||
pathChildren = "/children" | ||
pathConflicts = "/conflicts" | ||
pathConsumers = "/consumers" | ||
pathMetadata = "/metadata" | ||
pathInclusionState = "/inclusionState" | ||
pathConsensus = "/consensus" | ||
pathAttachments = "/attachments" | ||
) | ||
|
||
// GetAddressOutputs gets the spent and unspent outputs of an address. | ||
func (api *GoShimmerAPI) GetAddressOutputs(base58EncodedAddress string) (*json_models.GetAddressResponse, error) { | ||
res := &json_models.GetAddressResponse{} | ||
if err := api.do(http.MethodGet, func() string { | ||
return strings.Join([]string{routeGetAddresses, base58EncodedAddress}, "") | ||
}(), nil, res); err != nil { | ||
return nil, err | ||
} | ||
return res, nil | ||
} | ||
|
||
// GetAddressUnspentOutputs gets the unspent outputs of an address. | ||
func (api *GoShimmerAPI) GetAddressUnspentOutputs(base58EncodedAddress string) (*json_models.GetAddressResponse, error) { | ||
res := &json_models.GetAddressResponse{} | ||
if err := api.do(http.MethodGet, func() string { | ||
return strings.Join([]string{routeGetAddresses, base58EncodedAddress, pathUnspentOutputs}, "") | ||
}(), nil, res); err != nil { | ||
return nil, err | ||
} | ||
return res, nil | ||
} | ||
|
||
// PostAddressUnspentOutputs gets the unspent outputs of several addresses. | ||
func (api *GoShimmerAPI) PostAddressUnspentOutputs(base58EncodedAddresses []string) (*json_models.PostAddressesUnspentOutputsResponse, error) { | ||
res := &json_models.PostAddressesUnspentOutputsResponse{} | ||
if err := api.do(http.MethodPost, func() string { | ||
return strings.Join([]string{routeGetAddresses, "unspentOutputs"}, "") | ||
}(), &json_models.PostAddressesUnspentOutputsRequest{Addresses: base58EncodedAddresses}, res); err != nil { | ||
return nil, err | ||
} | ||
return res, nil | ||
} | ||
|
||
// GetBranch gets the branch information. | ||
func (api *GoShimmerAPI) GetBranch(base58EncodedBranchID string) (*json_models.Branch, error) { | ||
res := &json_models.Branch{} | ||
if err := api.do(http.MethodGet, func() string { | ||
return strings.Join([]string{routeGetBranches, base58EncodedBranchID}, "") | ||
}(), nil, res); err != nil { | ||
return nil, err | ||
} | ||
return res, nil | ||
} | ||
|
||
// GetBranchChildren gets the children of a branch. | ||
func (api *GoShimmerAPI) GetBranchChildren(base58EncodedBranchID string) (*json_models.GetBranchChildrenResponse, error) { | ||
res := &json_models.GetBranchChildrenResponse{} | ||
if err := api.do(http.MethodGet, func() string { | ||
return strings.Join([]string{routeGetBranches, base58EncodedBranchID, pathChildren}, "") | ||
}(), nil, res); err != nil { | ||
return nil, err | ||
} | ||
return res, nil | ||
} | ||
|
||
// GetBranchConflicts gets the conflict branches of a branch. | ||
func (api *GoShimmerAPI) GetBranchConflicts(base58EncodedBranchID string) (*json_models.GetBranchConflictsResponse, error) { | ||
res := &json_models.GetBranchConflictsResponse{} | ||
if err := api.do(http.MethodGet, func() string { | ||
return strings.Join([]string{routeGetBranches, base58EncodedBranchID, pathConflicts}, "") | ||
}(), nil, res); err != nil { | ||
return nil, err | ||
} | ||
return res, nil | ||
} | ||
|
||
// GetOutput gets the output corresponding to OutputID. | ||
func (api *GoShimmerAPI) GetOutput(base58EncodedOutputID string) (*json_models.Output, error) { | ||
res := &json_models.Output{} | ||
if err := api.do(http.MethodGet, func() string { | ||
return strings.Join([]string{routeGetOutputs, base58EncodedOutputID}, "") | ||
}(), nil, res); err != nil { | ||
return nil, err | ||
} | ||
return res, nil | ||
} | ||
|
||
// GetOutputConsumers gets the consumers of the output corresponding to OutputID. | ||
func (api *GoShimmerAPI) GetOutputConsumers(base58EncodedOutputID string) (*json_models.GetOutputConsumersResponse, error) { | ||
res := &json_models.GetOutputConsumersResponse{} | ||
if err := api.do(http.MethodGet, func() string { | ||
return strings.Join([]string{routeGetOutputs, base58EncodedOutputID, pathConsumers}, "") | ||
}(), nil, res); err != nil { | ||
return nil, err | ||
} | ||
return res, nil | ||
} | ||
|
||
// GetOutputMetadata gets the metadata of the output corresponding to OutputID. | ||
func (api *GoShimmerAPI) GetOutputMetadata(base58EncodedOutputID string) (*json_models.OutputMetadata, error) { | ||
res := &json_models.OutputMetadata{} | ||
if err := api.do(http.MethodGet, func() string { | ||
return strings.Join([]string{routeGetOutputs, base58EncodedOutputID, pathMetadata}, "") | ||
}(), nil, res); err != nil { | ||
return nil, err | ||
} | ||
return res, nil | ||
} | ||
|
||
// GetTransaction gets the transaction of the corresponding to TransactionID. | ||
func (api *GoShimmerAPI) GetTransaction(base58EncodedTransactionID string) (*json_models.Transaction, error) { | ||
res := &json_models.Transaction{} | ||
if err := api.do(http.MethodGet, func() string { | ||
return strings.Join([]string{routeGetTransactions, base58EncodedTransactionID}, "") | ||
}(), nil, res); err != nil { | ||
return nil, err | ||
} | ||
return res, nil | ||
} | ||
|
||
// GetTransactionMetadata gets metadata of the transaction corresponding to TransactionID. | ||
func (api *GoShimmerAPI) GetTransactionMetadata(base58EncodedTransactionID string) (*json_models.TransactionMetadata, error) { | ||
res := &json_models.TransactionMetadata{} | ||
if err := api.do(http.MethodGet, func() string { | ||
return strings.Join([]string{routeGetTransactions, base58EncodedTransactionID, pathMetadata}, "") | ||
}(), nil, res); err != nil { | ||
return nil, err | ||
} | ||
return res, nil | ||
} | ||
|
||
// GetTransactionInclusionState gets inclusion state of the transaction corresponding to TransactionID. | ||
func (api *GoShimmerAPI) GetTransactionInclusionState(base58EncodedTransactionID string) (*json_models.TransactionInclusionState, error) { | ||
res := &json_models.TransactionInclusionState{} | ||
if err := api.do(http.MethodGet, func() string { | ||
return strings.Join([]string{routeGetTransactions, base58EncodedTransactionID, pathInclusionState}, "") | ||
}(), nil, res); err != nil { | ||
return nil, err | ||
} | ||
return res, nil | ||
} | ||
|
||
// GetTransactionConsensusMetadata gets the consensus metadata of the transaction corresponding to TransactionID. | ||
func (api *GoShimmerAPI) GetTransactionConsensusMetadata(base58EncodedTransactionID string) (*json_models.TransactionConsensusMetadata, error) { | ||
res := &json_models.TransactionConsensusMetadata{} | ||
if err := api.do(http.MethodGet, func() string { | ||
return strings.Join([]string{routeGetTransactions, base58EncodedTransactionID, pathConsensus}, "") | ||
}(), nil, res); err != nil { | ||
return nil, err | ||
} | ||
return res, nil | ||
} | ||
|
||
// GetTransactionAttachments gets the attachments (messageIDs) of the transaction corresponding to TransactionID. | ||
func (api *GoShimmerAPI) GetTransactionAttachments(base58EncodedTransactionID string) (*json_models.GetTransactionAttachmentsResponse, error) { | ||
res := &json_models.GetTransactionAttachmentsResponse{} | ||
if err := api.do(http.MethodGet, func() string { | ||
return strings.Join([]string{routeGetTransactions, base58EncodedTransactionID, pathAttachments}, "") | ||
}(), nil, res); err != nil { | ||
return nil, err | ||
} | ||
return res, nil | ||
} | ||
|
||
// PostTransaction sends the transaction(bytes) to the Tangle and returns its transaction ID. | ||
func (api *GoShimmerAPI) PostTransaction(transactionBytes []byte) (*json_models.PostTransactionResponse, error) { | ||
res := &json_models.PostTransactionResponse{} | ||
if err := api.do(http.MethodPost, routePostTransactions, | ||
&json_models.PostTransactionRequest{TransactionBytes: transactionBytes}, res); err != nil { | ||
return nil, err | ||
} | ||
|
||
return res, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package wallet | ||
|
||
import ( | ||
"sort" | ||
"time" | ||
|
||
"github.com/iotaledger/goshimmer/packages/ledgerstate" | ||
) | ||
|
||
// TimedBalance represents a balance that is time dependent. | ||
type TimedBalance struct { | ||
Balance map[ledgerstate.Color]uint64 | ||
Time time.Time | ||
} | ||
|
||
// TimedBalanceSlice is a slice containing TimedBalances. | ||
type TimedBalanceSlice []*TimedBalance | ||
|
||
// Sort sorts the balances based on their Time. | ||
func (t TimedBalanceSlice) Sort() { | ||
sort.Slice(t, func(i, j int) bool { | ||
return t[i].Time.Before(t[j].Time) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.