Skip to content

Moving copy changes back into root #1885

Answered by jochem-brouwer
faustbrian asked this question in Q&A
Discussion options

You must be logged in to vote

Definitely! The trie has a root property. Without going in the technical details, you can think of this as a hash of the entire database, which is deterministic (order of entries does not matter, only the end state, the key/value pairs matter). You can use this to move the changes of any copied trie into your root trie.

const root = new CheckpointTrie(database)
const temp = root.copy()

const key = Buffer.from('01','hex')
const value = Buffer.from('02','hex')

async function test() {
    temp.checkpoint()
    try {
        await temp.put(key, value)
        await temp.commit()
    } catch {
        await temp.revert();
    }

    console.log(await root.get(key)) // null

    root.root = temp

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@faustbrian
Comment options

Answer selected by faustbrian
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants