diff options
author | Jhih-Ming Huang <jm.huang@cobinhood.com> | 2019-04-22 17:56:20 +0800 |
---|---|---|
committer | Jhih-Ming Huang <jm@dexon.org> | 2019-05-08 21:45:52 +0800 |
commit | bf103451425a0f7a986ff6dd1e2b7d1e478bd948 (patch) | |
tree | 7f7a1a91496be7403dbb73844e8379c2f0f5c541 /core/vm/sqlvm/common | |
parent | d0f2c8fac94f6917e4e997fd0b236d4de36e41d1 (diff) | |
download | dexon-bf103451425a0f7a986ff6dd1e2b7d1e478bd948.tar dexon-bf103451425a0f7a986ff6dd1e2b7d1e478bd948.tar.gz dexon-bf103451425a0f7a986ff6dd1e2b7d1e478bd948.tar.bz2 dexon-bf103451425a0f7a986ff6dd1e2b7d1e478bd948.tar.lz dexon-bf103451425a0f7a986ff6dd1e2b7d1e478bd948.tar.xz dexon-bf103451425a0f7a986ff6dd1e2b7d1e478bd948.tar.zst dexon-bf103451425a0f7a986ff6dd1e2b7d1e478bd948.zip |
core: vm: sqlvm: runtime: implement fillAutoInc and fillDefault
This commit implements fillAutoInc and fillDefault, and modifies
Storage.IncSequence such that it can handle the numbers larger than
maximum of uint64.
Diffstat (limited to 'core/vm/sqlvm/common')
-rw-r--r-- | core/vm/sqlvm/common/storage.go | 20 | ||||
-rw-r--r-- | core/vm/sqlvm/common/storage_test.go | 18 |
2 files changed, 21 insertions, 17 deletions
diff --git a/core/vm/sqlvm/common/storage.go b/core/vm/sqlvm/common/storage.go index f56e7984d..7babc04dd 100644 --- a/core/vm/sqlvm/common/storage.go +++ b/core/vm/sqlvm/common/storage.go @@ -5,6 +5,7 @@ import ( "golang.org/x/crypto/sha3" + "github.com/dexon-foundation/decimal" "github.com/dexon-foundation/dexon/common" "github.com/dexon-foundation/dexon/core/vm" "github.com/dexon-foundation/dexon/core/vm/sqlvm/schema" @@ -114,8 +115,8 @@ func (s *Storage) GetPrimaryPathHash(tableRef schema.TableRef) (h common.Hash) { return s.hashPathKey(key) } -// getSequencePathHash return the hash address of a sequence. -func (s *Storage) getSequencePathHash( +// GetSequencePathHash return the hash address of a sequence. +func (s *Storage) GetSequencePathHash( tableRef schema.TableRef, seqIdx uint8, ) common.Hash { // PathKey(["tables", "{table_name}", "sequence", uint8(sequence_idx)]) @@ -225,11 +226,14 @@ func (s *Storage) IncSequence( tableRef schema.TableRef, seqIdx uint8, inc uint64, -) uint64 { - seqPath := s.getSequencePathHash(tableRef, seqIdx) +) decimal.Decimal { + seqPath := s.GetSequencePathHash(tableRef, seqIdx) slot := s.GetState(contract, seqPath) - val := bytesToUint64(slot.Bytes()) - // TODO(yenlin): Check overflow? - s.SetState(contract, seqPath, common.BytesToHash(uint64ToBytes(val+inc))) - return val + b := new(big.Int).SetBytes(slot.Bytes()) + b.Add(b, new(big.Int).SetUint64(inc)) + newHash := make([]byte, common.HashLength) + bs := b.Bytes() + copy(newHash[common.HashLength-len(bs):], bs) + s.SetState(contract, seqPath, common.BytesToHash(newHash)) + return decimal.NewFromBigInt(b, 0) } diff --git a/core/vm/sqlvm/common/storage_test.go b/core/vm/sqlvm/common/storage_test.go index b02705c11..2bc1354e1 100644 --- a/core/vm/sqlvm/common/storage_test.go +++ b/core/vm/sqlvm/common/storage_test.go @@ -192,17 +192,17 @@ func (s *StorageTestSuite) TestSequence() { table2 := schema.TableRef(1) contract := common.BytesToAddress([]byte("A")) - s.Require().Equal(uint64(0), s.storage.IncSequence(contract, table1, 0, 2)) - s.Require().Equal(uint64(2), s.storage.IncSequence(contract, table1, 0, 1)) - s.Require().Equal(uint64(3), s.storage.IncSequence(contract, table1, 0, 1)) + s.Require().Equal(uint64(2), s.storage.IncSequence(contract, table1, 0, 2).Rescale(0).Coefficient().Uint64()) + s.Require().Equal(uint64(3), s.storage.IncSequence(contract, table1, 0, 1).Rescale(0).Coefficient().Uint64()) + s.Require().Equal(uint64(4), s.storage.IncSequence(contract, table1, 0, 1).Rescale(0).Coefficient().Uint64()) // Repeat on another sequence. - s.Require().Equal(uint64(0), s.storage.IncSequence(contract, table1, 1, 1)) - s.Require().Equal(uint64(1), s.storage.IncSequence(contract, table1, 1, 2)) - s.Require().Equal(uint64(3), s.storage.IncSequence(contract, table1, 1, 3)) + s.Require().Equal(uint64(1), s.storage.IncSequence(contract, table1, 1, 1).Rescale(0).Coefficient().Uint64()) + s.Require().Equal(uint64(3), s.storage.IncSequence(contract, table1, 1, 2).Rescale(0).Coefficient().Uint64()) + s.Require().Equal(uint64(6), s.storage.IncSequence(contract, table1, 1, 3).Rescale(0).Coefficient().Uint64()) // Repeat on another table. - s.Require().Equal(uint64(0), s.storage.IncSequence(contract, table2, 0, 3)) - s.Require().Equal(uint64(3), s.storage.IncSequence(contract, table2, 0, 4)) - s.Require().Equal(uint64(7), s.storage.IncSequence(contract, table2, 0, 5)) + s.Require().Equal(uint64(3), s.storage.IncSequence(contract, table2, 0, 3).Rescale(0).Coefficient().Uint64()) + s.Require().Equal(uint64(7), s.storage.IncSequence(contract, table2, 0, 4).Rescale(0).Coefficient().Uint64()) + s.Require().Equal(uint64(12), s.storage.IncSequence(contract, table2, 0, 5).Rescale(0).Coefficient().Uint64()) } func (s *StorageTestSuite) TestPKHeaderEncodeDecode() { |