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: Add types for ledgercore.StateDelta. #467

Merged
merged 16 commits into from
Feb 28, 2023
15 changes: 12 additions & 3 deletions client/v2/algod/getLedgerStateDelta.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,27 @@ import (
"fmt"

"github.com/algorand/go-algorand-sdk/v2/client/v2/common"
"github.com/algorand/go-algorand-sdk/v2/client/v2/common/models"
"github.com/algorand/go-algorand-sdk/v2/types"
)

// GetLedgerStateDeltaParams contains all of the query parameters for url serialization.
type GetLedgerStateDeltaParams struct {

// Format configures whether the response object is JSON or MessagePack encoded.
Format string `url:"format,omitempty"`
}

// GetLedgerStateDelta get ledger deltas for a round.
type GetLedgerStateDelta struct {
c *Client

round uint64

p GetLedgerStateDeltaParams
}

// Do performs the HTTP request
func (s *GetLedgerStateDelta) Do(ctx context.Context, headers ...*common.Header) (response models.LedgerStateDelta, err error) {
err = s.c.get(ctx, &response, fmt.Sprintf("/v2/deltas/%s", common.EscapeParams(s.round)...), nil, headers)
func (s *GetLedgerStateDelta) Do(ctx context.Context, headers ...*common.Header) (response types.LedgerStateDelta, err error) {
err = s.c.get(ctx, &response, fmt.Sprintf("/v2/deltas/%s", common.EscapeParams(s.round)...), s.p, headers)
shiqizng marked this conversation as resolved.
Show resolved Hide resolved
return
}
3 changes: 2 additions & 1 deletion client/v2/algod/rawTransaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import (
"github.com/algorand/go-algorand-sdk/v2/client/v2/common/models"
)

// SendRawTransaction broadcasts a raw transaction to the network.
// SendRawTransaction broadcasts a raw transaction or transaction group to the
// network.
type SendRawTransaction struct {
c *Client

Expand Down
10 changes: 0 additions & 10 deletions client/v2/common/models/account_balance_record.go

This file was deleted.

13 changes: 0 additions & 13 deletions client/v2/common/models/account_deltas.go

This file was deleted.

16 changes: 0 additions & 16 deletions client/v2/common/models/account_totals.go

This file was deleted.

22 changes: 0 additions & 22 deletions client/v2/common/models/app_resource_record.go

This file was deleted.

22 changes: 0 additions & 22 deletions client/v2/common/models/asset_resource_record.go

This file was deleted.

28 changes: 0 additions & 28 deletions client/v2/common/models/ledger_state_delta.go

This file was deleted.

13 changes: 0 additions & 13 deletions client/v2/common/models/modified_app.go

This file was deleted.

13 changes: 0 additions & 13 deletions client/v2/common/models/modified_asset.go

This file was deleted.

24 changes: 24 additions & 0 deletions client/v2/common/models/node_status_response.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,28 @@ type NodeStatusResponse struct {

// TimeSinceLastRound timeSinceLastRound in nanoseconds
TimeSinceLastRound uint64 `json:"time-since-last-round"`

// UpgradeDelay upgrade delay
UpgradeDelay uint64 `json:"upgrade-delay,omitempty"`

// UpgradeNextProtocolVoteBefore next protocol round
UpgradeNextProtocolVoteBefore uint64 `json:"upgrade-next-protocol-vote-before,omitempty"`

// UpgradeNoVotes no votes cast for consensus upgrade
UpgradeNoVotes uint64 `json:"upgrade-no-votes,omitempty"`

// UpgradeNodeVote this node's upgrade vote
UpgradeNodeVote bool `json:"upgrade-node-vote,omitempty"`

// UpgradeVoteRounds total voting ounds for current upgrade
UpgradeVoteRounds uint64 `json:"upgrade-vote-rounds,omitempty"`

// UpgradeVotes total votes cast for consensus upgrade
UpgradeVotes uint64 `json:"upgrade-votes,omitempty"`

// UpgradeVotesRequired yes votes required for consensus upgrade
UpgradeVotesRequired uint64 `json:"upgrade-votes-required,omitempty"`

// UpgradeYesVotes yes votes cast for consensus upgrade
UpgradeYesVotes uint64 `json:"upgrade-yes-votes,omitempty"`
}
13 changes: 0 additions & 13 deletions client/v2/common/models/tx_lease.go

This file was deleted.

5 changes: 5 additions & 0 deletions client/v2/indexer/indexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ const authHeader = "X-Indexer-API-Token"

type Client common.Client

// delete performs a DELETE request to the specific path against the server, assumes JSON response
func (c *Client) delete(ctx context.Context, response interface{}, path string, body interface{}, headers []*common.Header) error {
return (*common.Client)(c).Delete(ctx, response, path, body, headers)
}

// get performs a GET request to the specific path against the server, assumes JSON response
func (c *Client) get(ctx context.Context, response interface{}, path string, body interface{}, headers []*common.Header) error {
return (*common.Client)(c).Get(ctx, response, path, body, headers)
Expand Down
22 changes: 22 additions & 0 deletions encoding/json/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ var CodecHandle *codec.JsonHandle
// LenientCodecHandle is used to instantiate msgpack encoders for the REST API.
var LenientCodecHandle *codec.JsonHandle

// JSONStrictHandle is the same as CodecHandle but with MapKeyAsString=true
// for correct maps[int]interface{} encoding
var JSONStrictHandle *codec.JsonHandle

// init configures our json encoder and decoder
func init() {
CodecHandle = new(codec.JsonHandle)
Expand All @@ -30,6 +34,15 @@ func init() {
LenientCodecHandle.RecursiveEmptyCheck = true
LenientCodecHandle.Indent = 2
LenientCodecHandle.HTMLCharsAsIs = true

JSONStrictHandle = new(codec.JsonHandle)
JSONStrictHandle.ErrorIfNoField = CodecHandle.ErrorIfNoField
JSONStrictHandle.ErrorIfNoArrayExpand = CodecHandle.ErrorIfNoArrayExpand
JSONStrictHandle.Canonical = CodecHandle.Canonical
JSONStrictHandle.RecursiveEmptyCheck = CodecHandle.RecursiveEmptyCheck
JSONStrictHandle.Indent = CodecHandle.Indent
JSONStrictHandle.HTMLCharsAsIs = CodecHandle.HTMLCharsAsIs
JSONStrictHandle.MapKeyAsString = true
}

// Encode returns a json-encoded byte buffer for a given object
Expand All @@ -40,6 +53,15 @@ func Encode(obj interface{}) []byte {
return b
}

// EncodeStrict returns a JSON-encoded byte buffer for a given object
// It is the same Encode but encodes map's int keys as strings
func EncodeStrict(obj interface{}) []byte {
var b []byte
enc := codec.NewEncoderBytes(&b, JSONStrictHandle)
enc.MustEncode(obj)
return b
}

// Decode attempts to decode a json-encoded byte buffer into an
// object instance pointed to by objptr
func Decode(b []byte, objptr interface{}) error {
Expand Down
34 changes: 30 additions & 4 deletions encoding/json/json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

type object struct {
Expand All @@ -27,7 +28,7 @@ func TestDecode(t *testing.T) {
// basic encode/decode test.
var decoded object
err := Decode(encodedOb, &decoded)
assert.NoError(t, err)
require.NoError(t, err)
assert.Equal(t, obj, decoded)
})

Expand All @@ -36,7 +37,7 @@ func TestDecode(t *testing.T) {
decoder := NewDecoder(bytes.NewReader(encodedOb))
var decoded object
err := decoder.Decode(&decoded)
assert.NoError(t, err)
require.NoError(t, err)
assert.Equal(t, obj, decoded)
})

Expand All @@ -45,7 +46,7 @@ func TestDecode(t *testing.T) {
decoder := NewDecoder(bytes.NewReader(encodedOb))
var decoded subsetObject
err := decoder.Decode(&decoded)
assert.Error(t, err)
require.Error(t, err)
assert.Contains(t, err.Error(), "no matching struct field found when decoding stream map with key name")
})

Expand All @@ -54,7 +55,32 @@ func TestDecode(t *testing.T) {
decoder := NewLenientDecoder(bytes.NewReader(encodedOb))
var decoded subsetObject
err := decoder.Decode(&decoded)
assert.NoError(t, err)
require.NoError(t, err)
assert.Equal(t, obj.subsetObject, decoded)
})

t.Run("original encode map key as string", func(t *testing.T) {
intMap := map[int]string{
0: "int key",
}
data := string(Encode(intMap))
assert.NotContains(t, data, "\"0\":")
})

t.Run("strict encode map key as string", func(t *testing.T) {
intMap := map[int]string{
0: "int key",
}
data := string(EncodeStrict(intMap))
assert.NotContains(t, data, "0:")
})

t.Run("strict encode map interface key as string", func(t *testing.T) {
t.Skip("There is a bug in go-codec with MapKeyAsString = true and Canonical = true")
intMap := map[interface{}]interface{}{
0: "int key",
}
data := string(EncodeStrict(intMap))
assert.NotContains(t, data, "0:")
})
}
2 changes: 1 addition & 1 deletion test/responses_unit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ func theParsedResponseShouldEqualTheMockResponse() error {
if responseStr, ok := response.(string); ok {
responseJson = responseStr
} else {
responseJson = string(json.Encode(response))
responseJson = string(json.EncodeStrict(response))
}
}

Expand Down
1 change: 0 additions & 1 deletion test/unit.tags
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
@unit.sourcemap
@unit.stateproof.paths
@unit.stateproof.responses
@unit.stateproof.responses.msgp
@unit.statedelta
@unit.tealsign
@unit.transactions
Expand Down
Loading