github.com/aergoio/aergo@v1.3.1/state/statebuffer_test.go (about) 1 package state 2 3 import ( 4 "encoding/hex" 5 "testing" 6 7 "github.com/aergoio/aergo/types" 8 "github.com/stretchr/testify/assert" 9 ) 10 11 var ( 12 kset = []types.HashID{ 13 types.ToHashID([]byte{0x01}), 14 types.ToHashID([]byte{0x02}), 15 } 16 k0, k1 = kset[0], kset[1] 17 ) 18 19 func TestBufferIndexStack(t *testing.T) { 20 idxs := bufferIndex{} 21 22 idxs.push(k0, 0) 23 idxs.push(k0, 1) 24 idxs.push(k1, 2) 25 idxs.push(k0, 3) 26 idxs.push(k0, 4) 27 idxs.push(k1, 5) 28 for i, v := range kset { 29 t.Logf("(%d)[%v]%v", i, hex.EncodeToString(v[:]), idxs[v]) 30 } 31 32 assert.Equal(t, 4, idxs.pop(k0)) 33 assert.Equal(t, 3, idxs.pop(k0)) 34 assert.Equal(t, 1, idxs.pop(k0)) 35 assert.Equal(t, 0, idxs.pop(k0)) 36 assert.Equal(t, 5, idxs.peek(k1)) 37 assert.Equal(t, 5, idxs.peek(k1)) 38 assert.Equal(t, 5, idxs.pop(k1)) 39 assert.Equal(t, 2, idxs.peek(k1)) 40 assert.Equal(t, 2, idxs.pop(k1)) 41 for i, v := range kset { 42 t.Logf("(%d)[%v]%v", i, hex.EncodeToString(v[:]), idxs[v]) 43 } 44 45 idxs.push(k0, 6, 8, 12) 46 idxs.push(k1, 7, 9, 10, 11) 47 for i, v := range kset { 48 t.Logf("(%d)[%v]%v", i, hex.EncodeToString(v[:]), idxs[v]) 49 } 50 51 assert.Equal(t, 12, idxs[k0].peek()) 52 assert.Equal(t, 11, idxs[k1].peek()) 53 } 54 func TestBufferIndexRollback(t *testing.T) { 55 idxs := bufferIndex{} 56 57 idxs.push(k0, 0, 1, 3, 4, 6, 7, 8) 58 idxs.push(k1, 2, 5, 9) 59 for i, v := range kset { 60 t.Logf("(%d)[%v]%v", i, hex.EncodeToString(v[:]), idxs[v]) 61 } 62 63 assert.Equal(t, 8, idxs[k0].peek()) 64 assert.Equal(t, 9, idxs[k1].peek()) 65 66 idxs.rollback(5) 67 for i, v := range kset { 68 t.Logf("(%d)[%v]%v", i, hex.EncodeToString(v[:]), idxs[v]) 69 } 70 71 assert.Equal(t, 4, idxs[k0].peek()) 72 assert.Equal(t, 2, idxs[k1].peek()) 73 74 idxs.rollback(0) 75 for i, v := range kset { 76 t.Logf("(%d)[%v]%v", i, hex.EncodeToString(v[:]), idxs[v]) 77 } 78 79 assert.Equal(t, -1, idxs[k0].peek()) 80 assert.Equal(t, -1, idxs[k1].peek()) 81 } 82 83 func TestBufferRollback(t *testing.T) { 84 stb := newStateBuffer() 85 86 assert.Equal(t, 0, stb.snapshot()) 87 stb.put(newValueEntry(k0, []byte{1})) // rev 1: k0=1 88 stb.put(newValueEntry(k0, []byte{2})) // rev 2: k0=2 89 stb.put(newValueEntry(k0, []byte{3})) // rev 3: k0=3 90 stb.put(newValueEntry(k0, []byte{4})) // rev 4: k0=4 91 stb.put(newValueEntry(k1, []byte{1})) // rev 5: k0=4, k1=1 92 stb.put(newValueEntry(k1, []byte{2})) // rev 6: k0=4, k1=2 93 stb.put(newValueEntry(k1, []byte{3})) // rev 7: k0=4, k1=3 94 95 // snapshot revision 7 96 revision := stb.snapshot() // 7 97 assert.Equal(t, 7, stb.snapshot()) 98 99 stb.put(newValueEntry(k0, []byte{5})) // rev 8: k0=5, k1=3 100 stb.put(newValueEntry(k0, []byte{6})) // rev 9: k0=6, k1=3 101 assert.Equal(t, []byte{6}, stb.get(k0).Value()) 102 assert.Equal(t, []byte{3}, stb.get(k1).Value()) 103 t.Logf("k0: %v, k1: %v", stb.get(k0).Value(), stb.get(k1).Value()) 104 105 // rollback to revision 7 106 stb.rollback(revision) 107 assert.Equal(t, 7, stb.snapshot()) 108 109 assert.Equal(t, []byte{4}, stb.get(k0).Value()) 110 assert.Equal(t, []byte{3}, stb.get(k1).Value()) 111 t.Logf("k0: %v, k1: %v", stb.get(k0).Value(), stb.get(k1).Value()) 112 113 stb.put(newValueEntry(k1, []byte{4})) // rev 8: k0=4, k1=4 114 stb.put(newValueEntry(k1, []byte{5})) // rev 9: k0=4, k1=5 115 stb.put(newValueEntry(k0, []byte{7})) // rev 10: k0=7, k1=5 116 117 // snapshot revision 10 118 revision = stb.snapshot() // 10 119 assert.Equal(t, 10, stb.snapshot()) 120 121 stb.put(newValueEntry(k0, []byte{8})) // rev 11: k0=8, k1=5 122 stb.put(newValueEntry(k1, []byte{6})) // rev 12: k0=8, k1=6 123 assert.Equal(t, []byte{8}, stb.get(k0).Value()) 124 assert.Equal(t, []byte{6}, stb.get(k1).Value()) 125 t.Logf("k0: %v, k1: %v", stb.get(k0).Value(), stb.get(k1).Value()) 126 127 // rollback to revision 10 128 stb.rollback(revision) // 10 129 assert.Equal(t, 10, stb.snapshot()) 130 131 assert.Equal(t, []byte{7}, stb.get(k0).Value()) 132 assert.Equal(t, []byte{5}, stb.get(k1).Value()) 133 t.Logf("k0: %v, k1: %v", stb.get(k0).Value(), stb.get(k1).Value()) 134 } 135 136 func TestBufferHasKey(t *testing.T) { 137 stb := newStateBuffer() 138 assert.False(t, stb.has(k0)) 139 140 stb.put(newValueEntry(k0, []byte{1})) 141 assert.True(t, stb.has(k0)) // buffer has key 142 143 stb.put(newValueEntryDelete(k0)) 144 assert.True(t, stb.has(k0)) // buffer has key for ValueEntryDelete 145 146 stb.put(newValueEntry(k0, []byte{2})) 147 assert.True(t, stb.has(k0)) // buffer has key 148 149 stb.reset() 150 assert.False(t, stb.has(k0)) // buffer doesn't have key 151 }