-
Notifications
You must be signed in to change notification settings - Fork 208
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
WIP Support fungible invites directly #1870
Closed
Closed
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 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,169 @@ | ||
// @ts-check | ||
|
||
import Nat from '@agoric/nat'; | ||
import { passStyleOf } from '@agoric/marshal'; | ||
import { assert, details } from '@agoric/assert'; | ||
|
||
const identity = harden({ strings: [], counts: [] }); | ||
|
||
const assertUniqueSorted = strings => { | ||
const len = strings.length; | ||
for (let i = 1; i < len; i += 1) { | ||
const leftStr = strings[i - 1]; | ||
const rightStr = strings[i]; | ||
assert(leftStr !== rightStr, details`value has duplicates: ${strings}`); | ||
assert(leftStr < rightStr, details`value not sorted ${strings}`); | ||
} | ||
}; | ||
|
||
/** | ||
* Operations for arrays with unique string elements. More information | ||
* about these assets might be provided by some other mechanism, such as | ||
* an off-chain API or physical records. strBagMathHelpers are highly | ||
* efficient, but if the users rely on an external resource to learn | ||
* more about the digital assets (for example, looking up a string ID | ||
* in a database), the administrator of the external resource could | ||
* potentially change the external definition at any time. | ||
* | ||
* @type {MathHelpers} | ||
*/ | ||
const strBagMathHelpers = harden({ | ||
doCoerce: bag => { | ||
const { strings, counts } = bag; | ||
assert(passStyleOf(strings) === 'copyArray', 'value must be an array'); | ||
assert(passStyleOf(counts) === 'copyArray', 'value must be an array'); | ||
assert.equal(strings.length, counts.length); | ||
strings.forEach(str => assert.typeof(str, 'string')); | ||
counts.forEach(count => Nat(count) && assert(count >= 1)); | ||
assertUniqueSorted(strings); | ||
return harden(bag); | ||
}, | ||
doGetEmpty: _ => identity, | ||
doIsEmpty: ({ strings }) => | ||
passStyleOf(strings) === 'copyArray' && strings.length === 0, | ||
doIsGTE: (leftBag, rightBag) => { | ||
const { strings: leftStrings, counts: leftCounts } = leftBag; | ||
const { strings: rightStrings, counts: rightCounts } = rightBag; | ||
let leftI = 0; | ||
let rightI = 0; | ||
const leftLen = leftStrings.length; | ||
const rightLen = rightStrings.length; | ||
while (leftI < leftLen && rightI < rightLen) { | ||
const leftStr = leftStrings[leftI]; | ||
const rightStr = rightStrings[rightI]; | ||
if (leftStr < rightStr) { | ||
// elements of left not in right. Fine | ||
leftI += 1; | ||
} else if (leftStr > rightStr) { | ||
// elements of right not in left. | ||
return false; | ||
} else if (leftCounts[leftI] < rightCounts[rightI]) { | ||
// more of this element on right than in left. | ||
return false; | ||
} else { | ||
leftI += 1; | ||
rightI += 1; | ||
} | ||
} | ||
// Are there no elements of right remaining? | ||
return rightI >= rightLen; | ||
}, | ||
doIsEqual: (leftBag, rightBag) => { | ||
const { strings: leftStrings, counts: leftCounts } = leftBag; | ||
const { strings: rightStrings, counts: rightCounts } = rightBag; | ||
if (leftStrings.length !== rightStrings.length) { | ||
return false; | ||
} | ||
return leftStrings.every( | ||
(leftStr, i) => | ||
leftStr === rightStrings[i] && leftCounts[i] === rightCounts[i], | ||
); | ||
}, | ||
doAdd: (leftBag, rightBag) => { | ||
const { strings: leftStrings, counts: leftCounts } = leftBag; | ||
const { strings: rightStrings, counts: rightCounts } = rightBag; | ||
const resultStrings = []; | ||
const resultCounts = []; | ||
let leftI = 0; | ||
let rightI = 0; | ||
const leftLen = leftStrings.length; | ||
const rightLen = rightStrings.length; | ||
while (leftI < leftLen && rightI < rightLen) { | ||
const leftStr = leftStrings[leftI]; | ||
const rightStr = rightStrings[rightI]; | ||
if (leftStr < rightStr) { | ||
resultStrings.push(leftStr); | ||
resultCounts.push(leftCounts[leftI]); | ||
leftI += 1; | ||
} else if (leftStr > rightStr) { | ||
resultStrings.push(rightStr); | ||
resultCounts.push(rightCounts[rightI]); | ||
rightI += 1; | ||
} else { | ||
resultStrings.push(leftStr); | ||
resultCounts.push(Nat(leftCounts[leftI] + rightCounts[rightI])); | ||
leftI += 1; | ||
rightI += 1; | ||
} | ||
} | ||
if (leftI < leftLen) { | ||
resultStrings.push(leftStrings[leftI]); | ||
resultCounts.push(leftCounts[leftI]); | ||
} else if (rightI < rightLen) { | ||
resultStrings.push(rightStrings[rightI]); | ||
resultCounts.push(rightCounts[rightI]); | ||
} | ||
return harden({ strings: resultStrings, counts: resultCounts }); | ||
}, | ||
doSubtract: (leftBag, rightBag) => { | ||
const { strings: leftStrings, counts: leftCounts } = leftBag; | ||
const { strings: rightStrings, counts: rightCounts } = rightBag; | ||
const resultStrings = []; | ||
const resultCounts = []; | ||
let leftI = 0; | ||
let rightI = 0; | ||
const leftLen = leftStrings.length; | ||
const rightLen = rightStrings.length; | ||
while (leftI < leftLen && rightI < rightLen) { | ||
const leftStr = leftStrings[leftI]; | ||
const rightStr = rightStrings[rightI]; | ||
assert( | ||
leftStr <= rightStr, | ||
details`element of right not present in left ${rightStr}`, | ||
); | ||
if (leftStr < rightStr) { | ||
// an element of left not in right. Keep. | ||
resultStrings.push(leftStr); | ||
resultCounts.push(leftCounts[leftI]); | ||
leftI += 1; | ||
} else { | ||
// element in both. | ||
const leftCount = leftCounts[leftI]; | ||
const rightCount = rightCounts[rightI]; | ||
assert( | ||
leftCount >= rightCount, | ||
details`more of element on right than left ${rightStr},${rightCount}`, | ||
); | ||
const resultCount = Nat(leftCount - rightCount); | ||
if (resultCount >= 1) { | ||
resultStrings.push(leftStr); | ||
resultCounts.push(resultCount); | ||
} | ||
leftI += 1; | ||
rightI += 1; | ||
} | ||
} | ||
assert( | ||
rightI >= rightLen, | ||
details`some of the elements in right (${rightStrings}) were not present in left (${leftStrings})`, | ||
); | ||
if (leftI < leftLen) { | ||
resultStrings.push(leftStrings[leftI]); | ||
resultCounts.push(leftCounts[leftI]); | ||
} | ||
return harden({ strings: resultStrings, counts: resultCounts }); | ||
}, | ||
}); | ||
|
||
harden(strBagMathHelpers); | ||
export default strBagMathHelpers; |
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.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think these leftover processing loops should be while loops. Also the two in
doSubtract
below.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm. not sure. need closer examination than I'm about to give it.