-
Notifications
You must be signed in to change notification settings - Fork 12
feat(v3): add GraftConfigTreeNode to materialize anonymous config tree nodes #370
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
Merged
+210
−0
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,46 @@ | ||
| package v3 | ||
|
|
||
| import ( | ||
| "github.com/0xsequence/ethkit/go-ethereum/common" | ||
| ) | ||
|
|
||
| // GraftConfigTreeNode returns tree with every anonymous node-hash leaf whose | ||
| // image hash equals replacement.ImageHash() swapped for replacement, leaving | ||
| // the tree's image hash unchanged. ok reports whether the hash was found, | ||
| // either as a node leaf (replaced) or already as a full subtree (returned | ||
| // unchanged). | ||
| func GraftConfigTreeNode(tree WalletConfigTree, replacement WalletConfigTree) (grafted WalletConfigTree, ok bool) { | ||
| if tree == nil || replacement == nil { | ||
| return tree, false | ||
| } | ||
| return graftConfigTreeNode(tree, replacement, replacement.ImageHash().Hash) | ||
| } | ||
|
|
||
| func graftConfigTreeNode(tree WalletConfigTree, replacement WalletConfigTree, hash common.Hash) (WalletConfigTree, bool) { | ||
| if tree == nil { | ||
| return tree, false | ||
| } | ||
|
|
||
| if tree.ImageHash().Hash == hash { | ||
| switch tree.(type) { | ||
| case WalletConfigTreeNodeLeaf, *WalletConfigTreeNodeLeaf: | ||
| return replacement, true | ||
| } | ||
| return tree, true | ||
| } | ||
|
|
||
| switch n := tree.(type) { | ||
| case *WalletConfigTreeNode: | ||
| left, leftOk := graftConfigTreeNode(n.Left, replacement, hash) | ||
| right, rightOk := graftConfigTreeNode(n.Right, replacement, hash) | ||
| if leftOk || rightOk { | ||
| return &WalletConfigTreeNode{Left: left, Right: right}, true | ||
| } | ||
| case *WalletConfigTreeNestedLeaf: | ||
| if inner, ok := graftConfigTreeNode(n.Tree, replacement, hash); ok { | ||
| return &WalletConfigTreeNestedLeaf{Weight: n.Weight, Threshold: n.Threshold, Tree: inner}, true | ||
| } | ||
| } | ||
|
|
||
| return tree, false | ||
| } | ||
This file contains hidden or 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,164 @@ | ||
| package v3_test | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/0xsequence/ethkit/go-ethereum/common" | ||
| "github.com/0xsequence/go-sequence/core" | ||
| v3 "github.com/0xsequence/go-sequence/core/v3" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func graftTestSapientLeaf() *v3.WalletConfigTreeSapientSignerLeaf { | ||
| return &v3.WalletConfigTreeSapientSignerLeaf{ | ||
| Weight: 1, | ||
| Address: common.HexToAddress("0x9999999999999999999999999999999999999999"), | ||
| ImageHash_: core.ImageHash{Hash: common.HexToHash("0x0000000000000000000000000000000000000000000000000000000000000042")}, | ||
| } | ||
| } | ||
|
|
||
| // anonymousNodeLeaf mimics a decoded node-hash leaf: hash only, no preimage. | ||
| func anonymousNodeLeaf(subtree v3.WalletConfigTree) core.ImageHash { | ||
| return core.ImageHash{Hash: subtree.ImageHash().Hash} | ||
| } | ||
|
|
||
| func TestGraftConfigTreeNodeValueLeaf(t *testing.T) { | ||
| replacement := graftTestSapientLeaf() | ||
|
|
||
| tree := &v3.WalletConfigTreeNode{ | ||
| Left: &v3.WalletConfigTreeAddressLeaf{Weight: 2, Address: common.HexToAddress("0x1111111111111111111111111111111111111111")}, | ||
| Right: &v3.WalletConfigTreeNode{ | ||
| Left: v3.WalletConfigTreeNodeLeaf{Node: anonymousNodeLeaf(replacement)}, | ||
| Right: &v3.WalletConfigTreeAddressLeaf{Weight: 1, Address: common.HexToAddress("0x2222222222222222222222222222222222222222")}, | ||
| }, | ||
| } | ||
|
|
||
| grafted, ok := v3.GraftConfigTreeNode(tree, replacement) | ||
| require.True(t, ok) | ||
| require.Equal(t, tree.ImageHash().Hash, grafted.ImageHash().Hash) | ||
|
|
||
| node, ok := grafted.(*v3.WalletConfigTreeNode) | ||
| require.True(t, ok) | ||
| inner, ok := node.Right.(*v3.WalletConfigTreeNode) | ||
| require.True(t, ok) | ||
| require.Same(t, replacement, inner.Left) | ||
|
|
||
| // The input tree is not mutated. | ||
| require.IsType(t, v3.WalletConfigTreeNodeLeaf{}, tree.Right.(*v3.WalletConfigTreeNode).Left) | ||
| } | ||
|
|
||
| func TestGraftConfigTreeNodePointerLeaf(t *testing.T) { | ||
| replacement := graftTestSapientLeaf() | ||
|
|
||
| tree := &v3.WalletConfigTreeNode{ | ||
| Left: &v3.WalletConfigTreeNodeLeaf{Node: anonymousNodeLeaf(replacement)}, | ||
| Right: &v3.WalletConfigTreeAddressLeaf{Weight: 1, Address: common.HexToAddress("0x2222222222222222222222222222222222222222")}, | ||
| } | ||
|
|
||
| grafted, ok := v3.GraftConfigTreeNode(tree, replacement) | ||
| require.True(t, ok) | ||
| require.Equal(t, tree.ImageHash().Hash, grafted.ImageHash().Hash) | ||
|
|
||
| node, ok := grafted.(*v3.WalletConfigTreeNode) | ||
| require.True(t, ok) | ||
| require.Same(t, replacement, node.Left) | ||
| } | ||
|
|
||
| func TestGraftConfigTreeNodeNestedLeaf(t *testing.T) { | ||
| // Non-sapient replacement: a plain subtree of address leaves. | ||
| replacement := &v3.WalletConfigTreeNode{ | ||
| Left: &v3.WalletConfigTreeAddressLeaf{Weight: 1, Address: common.HexToAddress("0x3333333333333333333333333333333333333333")}, | ||
| Right: &v3.WalletConfigTreeAddressLeaf{Weight: 1, Address: common.HexToAddress("0x4444444444444444444444444444444444444444")}, | ||
| } | ||
|
|
||
| tree := &v3.WalletConfigTreeNestedLeaf{ | ||
| Weight: 3, | ||
| Threshold: 2, | ||
| Tree: v3.WalletConfigTreeNodeLeaf{Node: anonymousNodeLeaf(replacement)}, | ||
| } | ||
|
|
||
| grafted, ok := v3.GraftConfigTreeNode(tree, replacement) | ||
| require.True(t, ok) | ||
| require.Equal(t, tree.ImageHash().Hash, grafted.ImageHash().Hash) | ||
|
|
||
| nested, ok := grafted.(*v3.WalletConfigTreeNestedLeaf) | ||
| require.True(t, ok) | ||
| require.Equal(t, uint8(3), nested.Weight) | ||
| require.Equal(t, uint16(2), nested.Threshold) | ||
| require.Same(t, replacement, nested.Tree) | ||
| } | ||
|
|
||
| func TestGraftConfigTreeNodeAlreadyPresent(t *testing.T) { | ||
| present := graftTestSapientLeaf() | ||
|
|
||
| tree := &v3.WalletConfigTreeNode{ | ||
| Left: &v3.WalletConfigTreeAddressLeaf{Weight: 1, Address: common.HexToAddress("0x1111111111111111111111111111111111111111")}, | ||
| Right: present, | ||
| } | ||
|
|
||
| grafted, ok := v3.GraftConfigTreeNode(tree, graftTestSapientLeaf()) | ||
| require.True(t, ok) | ||
| require.Equal(t, tree, grafted) | ||
| require.Same(t, present, grafted.(*v3.WalletConfigTreeNode).Right) | ||
|
|
||
| // The whole tree matching the replacement is also left unchanged. | ||
| grafted, ok = v3.GraftConfigTreeNode(present, graftTestSapientLeaf()) | ||
| require.True(t, ok) | ||
| require.Same(t, present, grafted) | ||
| } | ||
|
|
||
| // TestGraftConfigTreeNodeAllOccurrences covers the duplicate-hash case: an | ||
| // already-materialized occurrence must not stop the traversal from grafting a | ||
| // later anonymous occurrence, and multiple anonymous occurrences are all | ||
| // replaced. | ||
| func TestGraftConfigTreeNodeAllOccurrences(t *testing.T) { | ||
| replacement := graftTestSapientLeaf() | ||
|
|
||
| // Materialized on the left, anonymous on the right. | ||
| tree := &v3.WalletConfigTreeNode{ | ||
| Left: graftTestSapientLeaf(), | ||
| Right: v3.WalletConfigTreeNodeLeaf{Node: anonymousNodeLeaf(replacement)}, | ||
| } | ||
| grafted, ok := v3.GraftConfigTreeNode(tree, replacement) | ||
| require.True(t, ok) | ||
| require.Equal(t, tree.ImageHash().Hash, grafted.ImageHash().Hash) | ||
| node, isNode := grafted.(*v3.WalletConfigTreeNode) | ||
| require.True(t, isNode) | ||
| require.Same(t, replacement, node.Right) | ||
|
|
||
| // Two anonymous occurrences: both replaced. | ||
| tree = &v3.WalletConfigTreeNode{ | ||
| Left: &v3.WalletConfigTreeNodeLeaf{Node: anonymousNodeLeaf(replacement)}, | ||
| Right: v3.WalletConfigTreeNodeLeaf{Node: anonymousNodeLeaf(replacement)}, | ||
| } | ||
| grafted, ok = v3.GraftConfigTreeNode(tree, replacement) | ||
| require.True(t, ok) | ||
| require.Equal(t, tree.ImageHash().Hash, grafted.ImageHash().Hash) | ||
| node, isNode = grafted.(*v3.WalletConfigTreeNode) | ||
| require.True(t, isNode) | ||
| require.Same(t, replacement, node.Left) | ||
| require.Same(t, replacement, node.Right) | ||
| } | ||
|
|
||
| func TestGraftConfigTreeNodeNotFound(t *testing.T) { | ||
| tree := &v3.WalletConfigTreeNode{ | ||
| Left: &v3.WalletConfigTreeAddressLeaf{Weight: 1, Address: common.HexToAddress("0x1111111111111111111111111111111111111111")}, | ||
| Right: v3.WalletConfigTreeNodeLeaf{Node: core.ImageHash{Hash: common.HexToHash("0x00000000000000000000000000000000000000000000000000000000000000ff")}}, | ||
| } | ||
|
|
||
| grafted, ok := v3.GraftConfigTreeNode(tree, graftTestSapientLeaf()) | ||
| require.False(t, ok) | ||
| require.Same(t, tree, grafted) | ||
| } | ||
|
|
||
| func TestGraftConfigTreeNodeNil(t *testing.T) { | ||
| tree := &v3.WalletConfigTreeAddressLeaf{Weight: 1, Address: common.HexToAddress("0x1111111111111111111111111111111111111111")} | ||
|
|
||
| grafted, ok := v3.GraftConfigTreeNode(nil, graftTestSapientLeaf()) | ||
| require.False(t, ok) | ||
| require.Nil(t, grafted) | ||
|
|
||
| grafted, ok = v3.GraftConfigTreeNode(tree, nil) | ||
| require.False(t, ok) | ||
| require.Same(t, tree, grafted) | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.