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

BugFix: Src map type assert fix #370

Merged
merged 8 commits into from
Aug 30, 2022
Merged

BugFix: Src map type assert fix #370

merged 8 commits into from
Aug 30, 2022

Conversation

barnjamin
Copy link
Contributor

It turns out reading json from disk and unmarshalling it produces a float whereas the result from the API returns a uint64.

func main() {

	b, err := os.ReadFile("../sourcemap.json")
	if err != nil {
		log.Fatalf("%+v", err)
	}

	m := map[string]interface{}{}
	err = json.Unmarshal(b, &m)
	if err != nil {
		log.Fatalf("fudge: %+v", err)
	}

        // Works
	mp, err := logic.DecodeSourceMap(m)
	if err != nil {
		log.Fatalf("fudge %+v", err)
	}
	log.Printf("%+v", mp)

	client, err := algod.MakeClient("http://localhost:4001", strings.Repeat("a", 64))
	if err != nil {
		log.Fatalf("fudge: %+v", err)
	}

	var program []byte = []byte("#pragma version 6\npushint 1\npop\nint 1\nreturn")

	result, err := client.TealCompile(program).Sourcemap(true).Do(context.Background())
	if err != nil {
		log.Fatalf("Failed to compile: %+v", err)
	}

        // Doesn't work (prior to change)
	mp, err = logic.DecodeSourceMap(*result.Sourcemap)
	if err != nil {
		log.Fatalf("fudge %+v", err)
	}

	log.Printf("%+v", mp)

}

@barnjamin barnjamin changed the title Src map type assert fix BugFix: Src map type assert fix Aug 23, 2022
@winder
Copy link
Contributor

winder commented Aug 29, 2022

Why are you unmarshaling to a map[string]interface{} instead of a structured object?

@@ -22,7 +22,12 @@ func DecodeSourceMap(ism map[string]interface{}) (SourceMap, error) {
sm := SourceMap{}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alternative:

	buf:=json.Encode(ism)
	json.Decode(buf,&sm)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you help me understand why this is better?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The only reason I bring it up, is that it's easy to get things wrong with reflection. So if there's a way to avoid using it the code would be easier to reason about and less likely that there is an error. So basically, I'm asking if these are simpler alternatives.

Why are you unmarshaling to a map[string]interface{} instead of a structured object?

What I mean is you could use Unmarshal into a real type instead of map[string]interface{}, that way Unmarshal would handle all of this logic. Looking a bit closer just now, I wonder if this is even better:

func DecodeSourceMap(ism map[string]interface{}) (SourceMap, error) {
  buf := json.Encode(ism)
  var sourcemap SourceMap
  return json.Decode(buf, sourcemap)
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can do that to begin with to set the fields specified but I still need to do some post-processing to decode the VLQ values. Is it worth it?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it is, but not enough to block your PR so I'll leave it up to you.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm going to merge this and post a follow up with the above since I think this is tripping folks up right now

@barnjamin
Copy link
Contributor Author

@winder

Why are you unmarshaling to a map[string]interface{} instead of a structured object?

You mean why does this method accept a map[string]interface{} ? If so, its just what we get from the call to compile endpoint: https://github.com/algorand/go-algorand-sdk/blob/develop/client/v2/common/models/compile_response.go#L12

@barnjamin barnjamin merged commit aa22fdf into develop Aug 30, 2022
@barnjamin barnjamin deleted the src-map-type-assert-fix branch August 30, 2022 18:47
ahangsu added a commit that referenced this pull request Sep 2, 2022
* Github-Actions: Adding pr title and label checks (#336)

* Dev Tools: Source map decoder (#335)

* Updated CHANGELOG.md

* AVM:  Consolidate TEAL and AVM versions (#345)

* Testing: Use Dev mode network for cucumber tests (#349)

* AVM: Use avm-abi repo (#352)

* Updated CHANGELOG.md

* Enhancement: Use Sandbox for Testing (#360)

Co-authored-by: Michael Diamant <[email protected]>

* Bug-Fix: passthru verbosity (#371)

* Enhancement: Deprecating use of langspec (#366)

Co-authored-by: Michael Diamant <[email protected]>

* StateProof: State proof support (#374)

* Regenerate client.

* Update txn and block objects, cucumber tests passing.

* Enable cucumber tests.

* Update types/stateproof.go

* Add transactions-root-256

* Remove unused code.

* Remove unused file

* Add txn commitments blockheader fields

* Rename signature, add allocbound definitions

* Add txn commitment to header

* PR comments

* Remove unused file

Co-authored-by: Will Winder <[email protected]>

* State Proofs: Use generic type for StateProof txn field. (#378)

* Use generic type for StateProof txn field.

* Add missing type.

* Remove GenericDigest.

* fix type assertion on source map version (#370)

* let the json package figure out how to decode the elements of the incoming map (#380)

* StateProof: State Proof Verification additions (#377)

* Regenerate client.

* Remove unused file

* moved files from stateproof verification repo

* now using updated go-stateproof-verification

* fixed go mod

* moved consts near their usage

* added working state proof verifier test

* added unit test and resources

* now using go 1.15 supported function

* changes to avoid dependency of stateproof verification on SDK

* added needed types to stateproof.go for message hashing

* reverted packages, now using up to date go-stateproof-verification

* CR: renamed verification functions

* hashing sp message is now in crypto

* removed hashing of light block header

* CR: now allocating space upfront in hashing state proof message

* attempting to use embedded files

* CR: using GenericDigest in block.go

* CR: Renamed package to match directory

* Removed go 1.15 from CI

* improved embed usage

* CR: removed stateproof verification logic

* tidied go mod file

Co-authored-by: Will Winder <[email protected]>
Co-authored-by: Eric Warehime <[email protected]>

* State Proofs: added compute leaf function for light block header to sdk (#382)

* added compute leaf function for light block header to sdk

* renamed blockheader256

* changed passed type to pointer

* reverted back to copying

* renamed light block header hash func (#383)

* Enable stpf cucumber unit tests (#386)

Co-authored-by: Jack <[email protected]>
Co-authored-by: Ben Guidarelli <[email protected]>
Co-authored-by: Jack Smith <[email protected]>
Co-authored-by: Michael Diamant <[email protected]>
Co-authored-by: algochoi <[email protected]>
Co-authored-by: Jason Paulos <[email protected]>
Co-authored-by: Barbara Poon <[email protected]>
Co-authored-by: Zeph Grunschlag <[email protected]>
Co-authored-by: Eric Warehime <[email protected]>
Co-authored-by: Will Winder <[email protected]>
Co-authored-by: Almog Tal <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants